我有一个Json的回归:
[
{
"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"
}
]使用这个站点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; }
}然后,在我的C#代码中,我编写如下:
RootObject test = JsonConvert.DeserializeObject<RootObject>(jsonResult);但是,我收到了这个错误:
无法将当前JSON对象(例如,{“名称”:“值”})反序列化为'System.Collections.Generic.List`1ConsoleAPP.Aplication‘类型,因为该类型需要一个JSON数组(例如,1,2,3)才能正确反序列化。要修复此错误,要么将JSON更改为JSON数组(例如,1,2,3),要么更改反序列化类型,使之成为可以从JSON对象反序列化的普通.NET类型(例如,不像整数这样的原始类型,而不是数组或列表之类的集合类型)。还可以将JsonObjectAttribute添加到类型中,以强制它从JSON对象反序列化。路径'aplication.version',第1行,位置227。
我读了一些关于这方面的小窍门,但对我没有帮助。例如:
发布于 2013-03-12 14:51:14
JSON是一个数组--它只包含一个项,但仍然是一个数组。如果您从大的JSON字符串中删除了前导和尾随的[和],那么它应该可以反序列化。
或者可以反序列化为RootObject[]而不是RootObject。
这两种方法都会奏效。
https://stackoverflow.com/questions/15364383
复制相似问题