使用Newtonsoft.Json解析Json数组对象

4
我有一个对象数组,格式如下的json。
{
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"
                         }
                        ]
      }
  ]
}

我正在这样反序列化:
var outObject = JsonConvert.DeserializeObject<Table>(response);

这是我正在反序列化的类:
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; }
}

这会触发异常:

无法将当前的 JSON 对象(例如 {"name":"value"})反序列化为类型“System.Collections.Generic.List`1[Data.Table]”,因为该类型需要一个 JSON 数组(例如 [1,2,3])才能正确反序列化。

要修复此错误,请将 JSON 更改为 JSON 数组(例如 [1,2,3]),或更改反序列化的类型,使其成为可以从 JSON 对象反序列化的普通 .NET 类型(例如不是整数等基本类型,也不是像数组或 List 这样的集合类型)。还可以向类型添加 JsonObjectAttribute 以强制从 JSON 对象反序列化。路径“accounts.github”,行 1,位置 129。

我做错了什么?

1个回答

5
您缺少一个类。请添加以下内容:
public class RootObject
{
    public List<Table> Table { get; set; }
}

然后,像这样反序列化:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接