我是C#的新手,我想使用NewtonSoft将JSON文件反序列化为API,但它会导致我出现此错误 Newtonsoft.Json.JsonSerializationException:'无法将当前JSON数组(例如[1,2,3])反序列化为类型'API.JSonConfiguration.exampleLibrary',因为该类型需要一个JSON对象(例如{“name”:“value”} )正确反序列化。
“要修复此错误,请将JSON更改为JSON对象(例如{”name“:”value“})或将反序列化类型更改为数组或实现集合接口的类型(例如ICollection,IList)像可以从JSON数组反序列化的List。也可以将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"
}
]
答案 0 :(得分:0)
尝试
JsonConvert.DeserializeObject<IEnumerable<exampleLibrary>>(Data);
您有一系列数据但未在JsonConvert或exampleLibrary类中指定(我可以从您发布的代码中看到)。