Stack Overflow:
我正在使用JSON.NET并且之前已经成功完成了,但是我现在遇到了令人讨厌的JSON,我似乎无法反序列化。很明显,问题在于如何构建类,因此我请求有人帮我计划一个JSON.NET可以成功反序列化的类。
以下是JSON字符串的结构:
[
{
"name":"",
"cost":"",
"cmc":"",
"loyalty":"",
"supertype":"",
"type":"",
"subtype":"",
"power":"",
"toughness":"",
"hand":"",
"life":"",
"rule":"",
"multi":"",
"set":
[ // Note: there could be an infinite number of sets
{
"setcode":"",
"rarity":"",
"number":"",
"artist":"",
"flavor":""
},
{ "setcode":"",
"rarity":"",
"number":"",
"artist":"",
"flavor":""
},
]
}
]
非常感谢Stack Overflow
答案 0 :(得分:2)
您希望拥有一个包含所有这些名称属性的类。当涉及到数组时,它实际上是一个对象数组,因此您需要创建另一个具有相同命名属性的类。类似的东西:
public class MainObject
{
public string name { get;set; }
// rest of the names as properties
public List<SetObject> @set { get;set; }
}
public class SetObject
{
public string setcode { get;set; }
// rest of your sets names as properties
}
此外,从您的JSON看起来它已经是一个数组,因此您需要将其反序列化为数组:
var objects = JsonConvert.DeserializeObject<MainObject[]>(/* your json here */);
这将为您提供一个反序列化的MainObject数组,就像您的JSON是一个数组一样。