C#JsonConvert错误
我有一份Json报税表:C#JsonConvert错误,c#,json,json.net,C#,Json,Json.net,我有一份Json报税表: [ { "url": "http://xxx.xxx.xxx", "code": 0, "aplication": { "version": [ { "silent": true, "checksum": "9aabad09b09459ac6c9e54f9ca4eb5c4", "size": 1250619, "force": true, "appl
[
{
"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"
}
]
使用此网站,我生成以下类:
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; }
}
公共类版本
{
公共bool静默{get;set;}
公共字符串校验和{get;set;}
公共整数大小{get;set;}
公共布尔强制{get;set;}
公共字符串apply_消息{get;set;}
公共int id{get;set;}
公共int id_应用程序{get;set;}
公共字符串编号{get;set;}
公共字符串新闻{get;set;}
公共bool自动{get;set;}
公共字符串安装{get;set;}
}
公共类应用
{
公共列表版本{get;set;}
公共字符串除法\u名称{get;set;}
公共字符串app_name{get;set;}
公共int id{get;set;}
公共int id_master{get;set;}
}
公共类根对象
{
公共字符串url{get;set;}
公共整数代码{get;set;}
公共应用程序应用程序{get;set;}
公共字符串消息{get;set;}
}
然后,在我的C代码中,我写下:
RootObject test = JsonConvert.DeserializeObject<RootObject>(jsonResult);
rootobjecttest=JsonConvert.DeserializeObject(jsonResult);
但是,我收到这个错误:
无法反序列化当前JSON对象(例如{“名称”:“值”})
进入类型“System.Collections.Generic.List`1[ConsoleAPP.application]”
因为该类型需要一个JSON数组(例如[1,2,3])来反序列化
正确地要修复此错误,请将JSON更改为JSON数组
(例如[1,2,3])或更改反序列化类型,使其成为正常类型
.NET类型(例如,不是integer之类的基元类型,也不是集合
可以从JSON反序列化的类型(如数组或列表)
对象还可以将JsonObjectAttribute添加到类型以强制它
从JSON对象反序列化。路径“应用程序版本”,第1行,
位置227
我读了一些关于这方面的提示,但对我没有帮助。例如:
您的JSON是一个数组-它只包含一项,但仍然是一个数组。如果从大JSON字符串中删除前导和尾随的
[
和]
,它应该可以反序列化
或者可以反序列化为RootObject[]
而不是RootObject
这两种方法都可以。我也读过这篇技巧。但不起作用。这里可能没有问题,但我想更改
版本的名称,因为.NET已经有了一个类。@Eduardo:那么你做错了什么。我已经删除了这些类。不起作用(我刚刚尝试了两种方法——用原始JSON反序列化到RootObject[]
,删除外括号并反序列化到RootObject
——都很好。