gpt4 book ai didi

c# - 使用 Newtonsoft.Json 解析 Json 数组对象

转载 作者:行者123 更新时间:2023-11-30 15:28:54 25 4
gpt4 key购买 nike

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

这会触发一个异常:

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.

我做错了什么?

最佳答案

你错过了一节课。添加这个:

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

然后,像这样反序列化:

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

关于c# - 使用 Newtonsoft.Json 解析 Json 数组对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24757179/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com