使用Json.net反序列化JSON对象数组

时间:2013-08-12 16:45:10

标签: c# json.net

我尝试使用以下示例结构的API为其返回的json

[
   {
      "customer":{
         "first_name":"Test",
         "last_name":"Account",
         "email":"test1@example.com",
         "organization":"",
         "reference":null,
         "id":3545134,
         "created_at":"2013-08-06T15:51:15-04:00",
         "updated_at":"2013-08-06T15:51:15-04:00",
         "address":"",
         "address_2":"",
         "city":"",
         "state":"",
         "zip":"",
         "country":"",
         "phone":""
      }
   },
   {
      "customer":{
         "first_name":"Test",
         "last_name":"Account2",
         "email":"test2@example.com",
         "organization":"",
         "reference":null,
         "id":3570462,
         "created_at":"2013-08-12T11:54:58-04:00",
         "updated_at":"2013-08-12T11:54:58-04:00",
         "address":"",
         "address_2":"",
         "city":"",
         "state":"",
         "zip":"",
         "country":"",
         "phone":""
      }
   }
]

JSON.net可以使用类似以下结构

的方式
{
    "customer": {
        ["field1" : "value", etc...],
        ["field1" : "value", etc...],
    }
}

但我无法弄清楚如何让它对所提供的结构感到满意。

使用默认的JsonConvert.DeserializeObject(内容)会产生正确的Customer数,但所有数据都为空。

做一些CustomerList(下面)会导致“无法反序列化当前的JSON数组”异常

public class CustomerList
{
    public List<Customer> customer { get; set; }
}

思想?

5 个答案:

答案 0 :(得分:165)

您可以创建一个新模型来反序列化您的Json CustomerJson

public class CustomerJson
{
    [JsonProperty("customer")]
    public Customer Customer { get; set; }
}

public class Customer
{
    [JsonProperty("first_name")]
    public string Firstname { get; set; }

    [JsonProperty("last_name")]
    public string Lastname { get; set; }

    ...
}

你可以轻松地反序列化你的json:

JsonConvert.DeserializeObject<List<CustomerJson>>(json);

希望它有所帮助!

文档:Serializing and Deserializing JSON

答案 1 :(得分:43)

对于那些不想创建任何模型的人,请使用以下代码:

var result = JsonConvert.DeserializeObject<
  List<Dictionary<string, 
    Dictionary<string, string>>>>(content);

答案 2 :(得分:1)

使用已接受的答案,您必须使用 <add key="StagingConnect3" value="Driver={SQL Server Native Client 11.0};Server=.; Database=SampleDatabase;Trusted_Connection=yes;"/> 访问每条记录,并且您需要额外的Customers[i].customer课程,这有点烦人。如果您不想这样做,可以使用以下内容:

CustomerJson

请注意,我使用的是public class CustomerList { [JsonConverter(typeof(MyListConverter))] public List<Customer> customer { get; set; } } ,而不是数组。现在创建以下类:

List<>

答案 3 :(得分:0)

对上述内容稍作修改。我的Json格式(验证为

{
    mycollection:{[
           {   
               property0:value,
               property1:value,
             },
             {   
               property0:value,
               property1:value,
             }
           ]

         }
       }

我使用AlexDev的响应,使每个孩子循环播放,从中创建读者

 public partial class myModel
{
    public static List<myModel> FromJson(string json) => JsonConvert.DeserializeObject<myModelList>(json, Converter.Settings).model;
}

 public class myModelList {
    [JsonConverter(typeof(myModelConverter))]
    public List<myModel> model { get; set; }

}

class myModelConverter : JsonConverter
{
    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        var token = JToken.Load(reader);
        var list = Activator.CreateInstance(objectType) as System.Collections.IList;
        var itemType = objectType.GenericTypeArguments[0];
        foreach (var child in token.Children())  //mod here
        {
            var newObject = Activator.CreateInstance(itemType);
            serializer.Populate(child.CreateReader(), newObject); //mod here
            list.Add(newObject);
        }
        return list;
    }

    public override bool CanConvert(Type objectType)
    {
        return objectType.IsGenericType && (objectType.GetGenericTypeDefinition() == typeof(List<>));
    }
    public override bool CanWrite => false;
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) => throw new NotImplementedException();

}

答案 4 :(得分:0)

从JC_VA进行进一步修改,获取他拥有的内容,然后将MyModelConverter替换为...

public class MyModelConverter : JsonConverter
{
    //objectType is the type as specified for List<myModel> (i.e. myModel)
    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        var token = JToken.Load(reader); //json from myModelList > model
        var list = Activator.CreateInstance(objectType) as System.Collections.IList; // new list to return
        var itemType = objectType.GenericTypeArguments[0]; // type of the list (myModel)
        if (token.Type.ToString() == "Object") //Object
        {
            var child = token.Children();
            var newObject = Activator.CreateInstance(itemType);
            serializer.Populate(token.CreateReader(), newObject);
            list.Add(newObject);
        }
        else //Array
        {
            foreach (var child in token.Children())
            {
                var newObject = Activator.CreateInstance(itemType);
                serializer.Populate(child.CreateReader(), newObject);
                list.Add(newObject);
            }
        }
        return list;

    }

    public override bool CanConvert(Type objectType)
    {
        return objectType.IsGenericType && (objectType.GetGenericTypeDefinition() == typeof(List<>));
    }
    public override bool CanWrite => false;
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) => throw new NotImplementedException();
}

这应该适用于任一json

myModelList{
 model: [{ ... object ... }]
}

myModelList{
 model: { ... object ... }
}

它们最终都像被解析一样被解析

myModelList{
 model: [{ ... object ... }]
}