反序列化JSON对象.net

时间:2014-02-25 15:02:11

标签: c# .net json

我有反序列化json对象的问题。我在stackoverflow上读了很多线程但是我找不到解决方案。我使用.net 2.0和Newtonsoft库。

下面是json string:

{
    "data": {
        "custom_fields": [{
            "field": "segmentid",
            "value": "B"
        },
        {
            "field": "subsegmentid",
            "value": "TM3"
        },
        {
            "field": "contactpersonid",
            "value": "000187_003"
        },
        {
            "field": "firstname",
            "value": "ZBIGNIEW"
        },
        {
            "field": "agreetment",
            "value": "1"
        },
        {
            "field": "contactname",
            "value": "ZBIGNIEW TESTOWY"
        },
        {
            "field": "decisionmaking",
            "value": "0"
        },
        {
            "field": "lastpurchase",
            "value": ""
        },
        {
            "field": "agethresholds",
            "value": "0"
        },
        {
            "field": "tendercust",
            "value": ""
        }],
        "email": "myself@example.com",
        "state": "1"
    },
    "status": "OK"
}

我开发了两个类,但我仍然遇到错误。

  

无法反序列化当前的JSON对象(例如{\“name \”:\“value \”})   进入类型'System.Collections.Generic.List`1 [FMIntegration.DataFM]'   因为该类型需要一个JSON数组(例如[1,2,3])来反序列化   正确。\ r \ n要修复此错误,请将JSON更改为JSON数组   (例如[1,2,3])或更改反序列化类型以使其正常   .NET类型(例如,不是整数的基本类型,不是集合   类似于数组或List的类型,可以从JSON反序列化   宾语。 JsonObjectAttribute也可以添加到类型中以强制它   从JSON对象反序列化。\ r \ n“'data.email'

var SubscriberGet = JsonConvert.DeserializeObject<SubscriberGet>(json); 

public class SubscriberGet
{
    [JsonProperty("status")]
    public string Status { get; set; }

    [JsonProperty("data")]
    public List<DataFM> Data { get; set; }
}

public class DataFM
{
    [JsonProperty("email")]
    public string Email { get; set; }

    [JsonProperty("custom_fields")]
    public List<String> custom_fields { get; set; }

    [JsonProperty("state")]
    public string State { get; set; }
}

2 个答案:

答案 0 :(得分:4)

自定义字段不是字符串列表,而是“字段和值”对的列表。

试试这个:

public class SubscriberGet
{
    [JsonProperty("status")]
    public string Status { get; set; }

    [JsonProperty("data")]
    public List<DataFM> Data { get; set; }
}

public class DataFM
{
    [JsonProperty("email")]
    public string Email { get; set; }

    [JsonProperty("custom_fields")]
    public List<CustomField> custom_fields { get; set; }

    [JsonProperty("state")]
    public string State { get; set; }
}
public class CustomField
{
    [JsonProperty("field")]
    public string field { get; set; }
    [JsonProperty("value")]
    public string value { get; set; }
}

答案 1 :(得分:0)

这是处理我的json字符串的正确对象(感谢L.B.的链接)

public class CustomField
{
    public string field { get; set; }
    public string value { get; set; }
}

public class Data
{
    public List<CustomField> custom_fields { get; set; }
    public string email { get; set; }
    public string state { get; set; }
}

public class RootObject
{
    public Data data { get; set; }
    public string status { get; set; }
}
相关问题