使用Newtonsoft.Json解析Json Array对象 [英] Parse Json Array objects using Newtonsoft.Json

查看:820
本文介绍了使用Newtonsoft.Json解析Json Array对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照以下格式在json中有这样的对象数组

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

我正在这样反序列化:

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; }
}

这会引发异常:

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

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.

要解决此错误,可以将JSON更改为JSON数组(例如[1,2,3]),也可以更改反序列化类型,使其成为普通的.NET类型(例如,不是整数之类的原始类型,而不是可以从JSON对象反序列化的集合类型(如数组或List).还可以将JsonObjectAttribute添加到类型中,以强制其从JSON对象反序列化.路径"accounts.github",第1行,位置129.

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.

我在做什么错了?

推荐答案

您缺少课程.添加此内容:

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);

这篇关于使用Newtonsoft.Json解析Json Array对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆