使用Newtonsoft.Json解析Json Array对象

时间:2014-07-15 11:35:01

标签: c# windows-phone-7 windows-phone-8 json.net

我在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])或更改反序列化类型,使其成为普通的.NET类型(例如,不是像整数这样的基本类型,而不是可以从JSON对象反序列化的集合类型,如数组或List)。 JsonObjectAttribute也可以添加到类型中以强制它从JSON对象反序列化。路径'accounts.github',第1行,第129位。

我做错了什么?

1 个答案:

答案 0 :(得分:3)

你错过了一堂课。加上这个:

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

然后,像这样反序列化:

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