我有这个杰森回报:
[
{
"url": "http://xxx.xxx.xxx",
"code": 0,
"aplication":
{
"version":
[
{
"silent": true,
"checksum": "9aabad09b09459ac6c9e54f9ca4eb5c4",
"size": 1250619,
"force": true,
"apply_message": "",
"id": 116,
"id_aplication": 4,
"number": "1.0.5.0",
"news": "",
"automatic": true,
"installation": "Setup.exe"
}
],
"division_name": "DNT",
"app_name": "MyApplication",
"id": 4,
"id_master": 0
},
"message": "New Application Found"
}
]
Run Code Online (Sandbox Code Playgroud)
使用此站点http://json2csharp.com/,我生成了以下类:
public class Version
{
public bool silent { get; set; }
public string checksum { get; set; }
public int size { get; set; }
public bool force { get; set; }
public string apply_message { get; set; }
public int id { get; set; }
public int id_aplication { get; set; }
public string number { get; set; }
public string news { get; set; }
public bool automatic { get; set; }
public string installation { get; set; }
}
public class Aplication
{
public List<Version> version { get; set; }
public string division_name { get; set; }
public string app_name { get; set; }
public int id { get; set; }
public int id_master { get; set; }
}
public class RootObject
{
public string url { get; set; }
public int code { get; set; }
public Aplication aplication { get; set; }
public string message { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
然后,在我的C#代码中,我这样写:
RootObject test = JsonConvert.DeserializeObject<RootObject>(jsonResult);
Run Code Online (Sandbox Code Playgroud)
但是,我收到此错误:
无法将当前JSON对象(例如{“ name”:“ value”})反序列化为类型'System.Collections.Generic.List`1 [ConsoleAPP.Aplication]',因为该类型需要JSON数组(例如[1,2, 3])正确反序列化。要解决此错误,可以将JSON更改为JSON数组(例如[1,2,3]),也可以更改反序列化类型,使其成为普通的.NET类型(例如,不像整数这样的原始类型,也不像这样的集合类型。数组或列表),可以从JSON对象反序列化。还可以将JsonObjectAttribute添加到类型中,以强制其从JSON对象反序列化。路径“ aplication.version”,第1行,位置227。
我阅读了一些有关此的提示,但对我没有帮助。例如:
您的JSON是一个数组-它仅包含一个项目,但仍然是一个数组。如果除去开头和结尾[,并]从大的JSON字符串,它应该反序列化的罚款。
或者您可以反序列化为RootObject[]而不是RootObject。
无论哪种方式都可以。