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

本文关键字:对象 数组 JSON 反序列化 Json net 使用 | 更新日期: 2023-09-27 18:27:26

我尝试使用一个 API,该 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(content( 会生成正确的客户数量,但所有数据均为空。

执行客户列表(如下(的操作会导致"无法反序列化当前 JSON 数组"异常

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

思潮?

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

您可以创建一个新模型来反序列化 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);

文档:序列化和反序列化 JSON

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

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

注意:这不适用于您的 JSON 字符串。这不是任何 JSON 结构的通用解决方案。

使用接受的答案,您必须使用 Customers[i].customer 访问每条记录,并且您需要一个额外的CustomerJson类,这有点烦人。如果不想这样做,可以使用以下内容:

public class CustomerList
{
    [JsonConverter(typeof(MyListConverter))]
    public List<Customer> customer { get; set; }
}

请注意,我使用的是List<>,而不是数组。现在创建以下类:

class MyListConverter : 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.Values())
        {
            var childToken = child.Children().First();
            var newObject = Activator.CreateInstance(itemType);
            serializer.Populate(childToken.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 格式,验证是

{
    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();
}

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 ... }]
}

简单:创建两个类,一个用于客户,另一个用于数组对象。
像这样的 Json 字符串:

{
  "fieldObjects": [
    {
      "Field1": "ValueField1_a",
      "Field2": "ValueField2_a"
    },
    {
      "Field1": "ValueField1_b",
      "Field2": "ValueField2_b"
    }
  ]
}

在 .net 中,您有字段对象的第一个类(父类(,它只包含一个属性列表(子类(。子类包含属性字段 1、字段 2,...
最后使用父类在 Json 字符串上使用反序列化对象,而不使用列表 ...
就这样

如今,您可以使用System.Text.Json和JsonSerializer类。

CustomerList customers = JsonSerializer.Deserialize<CustomerList>(json);

List<Customer> customers = JsonSerializer.Deserialize<List<Customer>>(json);

前者是您自己的 POCO 类,具有属性组合,可以是标量或数组。后者,用于反序列化特定的数组对象。