猿问

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

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

我试图使用一个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)会生成正确的Customer数,但所有数据都为null。

在CustomerList(下面)执行某些操作会导致“无法反序列化当前JSON数组”异常

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

思考?


月关宝盒
浏览 1223回答 3
3回答

慕村225694

您可以创建一个新模型来反序列化您的Json&nbsp;CustomerJson:public&nbsp;class&nbsp;CustomerJson{ &nbsp;&nbsp;&nbsp;&nbsp;[JsonProperty("customer")] &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;Customer&nbsp;Customer&nbsp;{&nbsp;get;&nbsp;set;&nbsp;}}public&nbsp;class&nbsp;Customer{ &nbsp;&nbsp;&nbsp;&nbsp;[JsonProperty("first_name")] &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;string&nbsp;Firstname&nbsp;{&nbsp;get;&nbsp;set;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;[JsonProperty("last_name")] &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;string&nbsp;Lastname&nbsp;{&nbsp;get;&nbsp;set;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;...}你可以轻松地反序列化你的json:JsonConvert.DeserializeObject<List<CustomerJson>>(json);希望能帮助到你 !

慕姐4208626

使用已接受的答案,你必须使用Customers[i].customer,访问每个记录,你需要一个额外的CustomerJson类,这有点烦人。如果您不想这样做,可以使用以下内容:public&nbsp;class&nbsp;CustomerList{ &nbsp;&nbsp;&nbsp;&nbsp;[JsonConverter(typeof(MyListConverter))] &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;List<Customer>&nbsp;customer&nbsp;{&nbsp;get;&nbsp;set;&nbsp;}}请注意,我使用的是a&nbsp;List<>,而不是数组。现在创建以下类:class&nbsp;MyListConverter&nbsp;:&nbsp;JsonConverter{ &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;override&nbsp;object&nbsp;ReadJson(JsonReader&nbsp;reader,&nbsp;Type&nbsp;objectType,&nbsp;object&nbsp;existingValue,&nbsp;JsonSerializer&nbsp;serializer) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;token&nbsp;=&nbsp;JToken.Load(reader); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;list&nbsp;=&nbsp;Activator.CreateInstance(objectType)&nbsp;as&nbsp;System.Collections.IList; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;itemType&nbsp;=&nbsp;objectType.GenericTypeArguments[0]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foreach&nbsp;(var&nbsp;child&nbsp;in&nbsp;token.Values()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;childToken&nbsp;=&nbsp;child.Children().First(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;newObject&nbsp;=&nbsp;Activator.CreateInstance(itemType); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;serializer.Populate(childToken.CreateReader(),&nbsp;newObject); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list.Add(newObject); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;list; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;override&nbsp;bool&nbsp;CanConvert(Type&nbsp;objectType) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;objectType.IsGenericType&nbsp;&&&nbsp;(objectType.GetGenericTypeDefinition()&nbsp;==&nbsp;typeof(List<>)); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;override&nbsp;bool&nbsp;CanWrite&nbsp;=>&nbsp;false; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;override&nbsp;void&nbsp;WriteJson(JsonWriter&nbsp;writer,&nbsp;object&nbsp;value,&nbsp;JsonSerializer&nbsp;serializer)&nbsp;=>&nbsp;throw&nbsp;new&nbsp;NotImplementedException();}

收到一只叮咚

对于那些不想创建任何模型的人,请使用以下代码:var&nbsp;result&nbsp;=&nbsp;JsonConvert.DeserializeObject< &nbsp;&nbsp;List<Dictionary<string,&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;Dictionary<string,&nbsp;string>>>>(content);
随时随地看视频慕课网APP
我要回答