我是C#的新手,我希望使用NewtonSoft将JSON文件反序列化到API中,但是它会导致错误Newtonsoft.Json.JsonSerializationException:‘无法反序列化当前的JSON数组(例如,1,2,3)到'API.JSonConfiguration.exampleLibrary’类型,因为该类型需要一个JSON对象(例如,{“名称”:“值”})才能正确地反序列化。
“要修复此错误,要么将JSON更改为JSON对象(例如,{"name":"value"}),要么将反序列化类型更改为数组,或者将实现集合接口(例如ICollection、IList)的类型改为可以从JSON数组反序列化的列表。JsonArrayAttribute也可以添加到类型中,以强制它从JSON数组反序列化。
路径'',第1行,位置1。“
string url = @"http://180.232.67.229/api/jfiller";
string Data = new WebClient().DownloadString(url
exampleLibrary json = JsonConvert.DeserializeObject<exampleLibrary>(Data);
MessageBox.Show(json.filler_id);exampleLibrary类:
public string filler_id { get; set; }
public string filler_title { get; set; }
public string filler_type { get; set; }JSON
[
{
"filler_id":"1",
"filler_title":"Star118 E-CarDemo",
"filler_type":"1",
"filler_file":"Star118CarTeaser1.mp4",
"filler_duration":"83",
"created_at":"2017-06-10 09:08:41",
"updated":"2017-06-10 09:08:41","status":"0"
},
{
"filler_id":"2",
"filler_title":"Star118",
"filler_type":"2",
"filler_file":"Star118Solar1.PNG",
"filler_duration":"10",
"created_at":"2017-06-10 09:09:26",
"updated":"2017-06-10 09:09:26","status":"0"
}
]发布于 2017-07-20 03:13:55
试一试
JsonConvert.DeserializeObject<IEnumerable<exampleLibrary>>(Data);您有一个数据数组,但是不要在您的JsonConvert或exampleLibrary类中指定它(我从您发布的代码中可以看到)。
https://stackoverflow.com/questions/45204345
复制相似问题