反序列化使用newtonsoft
我是C#的新手,我想使用NewtonSoft反序列化到API的JSON文件,但它导致我到这个错误 Newtonsoft.Json.JsonSerializationException:'无法反序列化当前的JSON数组(例如[1 ,2,3])转换为'API.JSonConfiguration.exampleLibrary'类型,因为类型需要JSON对象(例如{“name”:“value”})才能正确地反序列化。反序列化使用newtonsoft
“要解决此错误,请将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);
示例类库:
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"
}
]
尝试
JsonConvert.DeserializeObject<IEnumerable<exampleLibrary>>(Data);
你有一组数据,但不要在你的JsonConvert或在您的exampleLibrary类(我可以向你发布的代码看其指定)。
我得到一个错误: System.InvalidCastException:'无法强制类型的对象System.Collections.Generic.List '1 [API.JSonConfiguration.exampleLibrary]'键入'API.JSonConfiguration.exampleLibrary'。'当我使用exampleLibrary json =(exampleLibrary)JsonConvert.DeserializeObject
List
你可以发布你的JSON字符串吗? – Xela
[{“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”}] – Ran
你可以尝试从你的json字符串中删除“[”&“]”,并把你的json在这里:json2csharp.com,并解析你的json使用该特定的类 –