Parse Json Array objects using Newtonsoft.Json

 ̄綄美尐妖づ 提交于 2019-12-10 10:08:20

问题


I have an array of objects like this in json as per below format

{
Table: [
     {
      userstatus: [
                   {
                    STATUS: "TRUE",
                    PACK: "UM6MONTHPACK",
                    EXPIRY: "8/15/2014 1:00:03 PM",             
                   }
                  ]
      },

      {
      activeauctions: [
                       {
                         ISBILLED: "0",
                         AUCTION_ID: "24",
                         AUCTION_NAME: "Swimsuit",      
                       }
                      ]
     },

     {
      upcomingauctions: [
                         {
                            AUCTION_ID: "4",
                        AUCTION_NAME: "Jacqueline Fernandezs Handbag",
                            SKU: "4_20131120"
                         },
                         {
                           AUCTION_ID: "4",
                        AUCTION_NAME: "Jacqueline Fernandezs Handbag",
                            SKU: "4_20131120"
                         }
                        ]
      }
  ]
}

I am deserializing like this:

var outObject = JsonConvert.DeserializeObject<Table>(response);

Here are the classes I am deserializing into:

public class Userstatu
{
    [Newtonsoft.Json.JsonProperty(PropertyName = "STATUS")]
    public string STATUS { get; set; }

    [Newtonsoft.Json.JsonProperty(PropertyName = "PACK")]
    public string PACK { get; set; }

    [Newtonsoft.Json.JsonProperty(PropertyName = "EXPIRY")]
    public string EXPIRY { get; set; }      
}

public class Activeauction
{
    [Newtonsoft.Json.JsonProperty(PropertyName = "ISBILLED")]
    public string ISBILLED { get; set; }

    [Newtonsoft.Json.JsonProperty(PropertyName = "AUCTION_ID")]
    public string AUCTION_ID { get; set; }

    [Newtonsoft.Json.JsonProperty(PropertyName = "AUCTION_NAME")]
    public string AUCTION_NAME { get; set; }        
}

public class Upcomingauction
{
    [Newtonsoft.Json.JsonProperty(PropertyName = "AUCTION_ID")]
    public string AUCTION_ID { get; set; }

    [Newtonsoft.Json.JsonProperty(PropertyName = "AUCTION_NAME")]
    public string AUCTION_NAME { get; set; }

    [Newtonsoft.Json.JsonProperty(PropertyName = "SKU")]
    public string SKU { get; set; }
}

public class Table
{
    [Newtonsoft.Json.JsonProperty(PropertyName = "userstatus")]
    public List<Userstatu> userstatus { get; set; }

    [Newtonsoft.Json.JsonProperty(PropertyName = "activeauctions")]
    public List<Activeauction> activeauctions { get; set; }

    [Newtonsoft.Json.JsonProperty(PropertyName = "upcomingauctions")]
    public List<Upcomingauction> upcomingauctions { get; set; }
}

This fires an exception:

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Data.Table]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'accounts.github', line 1, position 129.

What am I doing wrong?


回答1:


You are missing a class. Add this:

public class RootObject
{
    public List<Table> Table { get; set; }
}

Then, deserialize like this:

var outObject = JsonConvert.DeserializeObject<RootObject>(response);


来源:https://stackoverflow.com/questions/24757179/parse-json-array-objects-using-newtonsoft-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!