[英]Cannot deserialize JSON Object into List
I have the below classes:我有以下课程:
public class Datum
{
public bool Prop1 { get; set; }
public bool Prop2 { get; set; }
public bool Prop3 { get; set; }
public bool Prop4 { get; set; }
public bool Prop5 { get; set; }
public bool Prop6 { get; set; }
}
public class Example
{
public IList<Datum> data { get; set; }
}
And I have the below json in variable:我在变量中有以下 json:
var json = @"{'data': [
{
'Prop1': true,
'Prop2': true,
'Prop3': true,
'Prop4': true,
'Prop5': true,
'Prop6': true
},
{
'Prop1': true,
'Prop2': true,
'Prop3': true,
'Prop4': true,
'Prop5': true,
'Prop6': false
},
{
'Prop1': false,
'Prop2': true,
'Prop3': true,
'Prop4': true,
'Prop5': false,
'Prop6': false
},
{
'Prop1': false,
'Prop2': true,
'Prop3': true,
'Prop4': false,
'Prop5': false,
'Prop6': false
}]
}";
I am Using Netonsoft JSON.NET to deserialize the JSON into C# Objects like below:我正在使用 Netonsoft JSON.NET 将 JSON 反序列化为 C# 对象,如下所示:
var items = JsonConvert.DeserializeObject<List<Example>>(json);
But I am getting the below error:但我收到以下错误:
Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (eg {"name":"value"}) into type 'System.Collections.Generic.List`1[ReadJson.Example]' because the type requires a JSON array (eg [1,2,3]) to deserialize correctly. Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (eg {"name":"value"}) into type 'System.Collections.Generic.List`1[ReadJson.Example]' because the type requires a JSON array (例如 [1,2,3])正确反序列化。 To fix this error either change the JSON to a JSON array (eg [1,2,3]) or change the deserialized type so that it is a normal .NET type (eg not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. To fix this error either change the JSON to a JSON array (eg [1,2,3]) or change the deserialized type so that it is a normal .NET type (eg not a primitive type like integer, not a collection type like an数组或列表)可以从 JSON object 反序列化。 JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. JsonObjectAttribute 也可以添加到类型中以强制它从 JSON object 反序列化。 Path 'data', line 1, position 8.'路径“数据”,第 1 行,position 8。
I ran the exact same code in some online c# editor and this just works fine.我在一些在线 c# 编辑器中运行了完全相同的代码,并且效果很好。 But when I try in my local Visual Studio it gives the error.但是当我在本地 Visual Studio 中尝试时,它会出现错误。 I am using Newtonsoft Version 12 and above.我正在使用 Newtonsoft 版本 12 及更高版本。
Can anybody explain me what is wrong with my code.谁能解释我的代码有什么问题。
Thanks in Advance.提前致谢。
You don't have a list of Example
.您没有Example
列表。 You have an Example
, singular.你有一个Example
,单数。 Example
has a data
property; Example
具有data
属性; your JSON only contains one instance of that property, and its enclosing object isn't in an array.您的 JSON 仅包含该属性的一个实例,并且其封闭的 object 不在数组中。
So,所以,
JsonConvert.DeserializeObject<Example>(json)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.