在 c# asp .net core 中将 json 反序列化为 IEnumerable
Deserialize json to IEnumerable in c# asp .net core
我有一个 json 对象。我想将它反序列化为 IEnumerable
类型的对象。
我在 asp.net Core 工作。
我被这个试过了:
IEnumerable div = (IEnumerable)JsonConvert.DeserializeObject(value, typeof(IEnumerable));
这里的值是 json 变量。
我遇到了这种类型的异常:
Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type
'System.Collections.IEnumerable' because the type requires a JSON
array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET
type (e.g. not a primitive type like integer, not a collection type
like an array or List) that can be deserialized from a JSON object.
JsonObjectAttribute can also be added to the type to force it to
deserialize from a JSON object.
Json 文件:
{
"name": "xx",
"place": {
"native": "aa",
"school": "ee",
"college": "dd"
},
"dob": "ss",
"ids": [
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
26,
27,
28,
29,
30,
31,
32,
33,
34,
35,
36,
37,
38,
39,
40,
41,
42,
43,
44,
45,
46,
47,
48,
49,
50,
51,
52,
53,
54,
55,
56,
57,
58,
59,
60,
61,
62,
63,
64,
65,
66,
67,
68,
69,
70,
71,
72,
73,
74,
75,
76,
77,
78,
79,
80,
81,
82,
83,
84,
85,
86,
87,
88,
89,
90,
91,
92,
93,
94,
95,
96,
97,
98,
99,
100,
101,
102,
103,
104,
105,
106,
107,
108,
109,
110,
111,
112,
113,
114,
115,
116,
117,
118,
119,
120
],
"mark": [
{
"10thmark": "345",
"12thmark": "1100"
},
{
"Diploma": "G",
"Diploma PG": "R"
},
{
"Ug": "D",
"PG": "E"
}
]
}
如何解决?
1) 如果你的json是一个对象
例如我的用户
public class MyUser
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
而你的json是
{
"Id": 1,
"Name": "Andrew",
"Description": "Software Engineer"
}
然后你可以使用下面的代码轻松反序列化
MyUser user = JsonConvert.DeserializeObject<MyUser>(data);
上面代码中的"data"是你的json文件数据。
2) 如果你的 json 是一个对象数组
例如MyUser 数组。
{
"Users": [
{ "Id": 1, "Name": "Andrew", "Description": "Software Engineer"},
{ "Id": 2, "Name": "Eddy", "Description": "Software Developer"},
{ "Id": 3, "Name": "Matthew", "Description": "Web Developer"}
]
}
那么你需要一个 IEnumerable 类型的视图模型 属性 MyUser 来反序列化
public class MyUserVM
{
public IEnumerable<MyUser> Users { get; set; }
}
然后你需要使用上面的视图模型反序列化
MyUserVM users = JsonConvert.DeserializeObject<MyUserVM>(data);
上面代码中的"data"是你的json文件数据。
Q) 为什么这里需要视图模型?
A) 因为 json 是基于文本的键值格式数据,您需要一个键才能在 JsonConvert class.
中读取它
已编辑:
请像下面这样更改 json 密钥
{
"name": "xx",
"place": {
"native": "aa",
"school": "ee",
"college": "dd"
},
"dob": "ss",
"ids": [
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
26,
27,
28,
29,
30,
31,
32,
33,
34,
35,
36,
37,
38,
39,
40,
41,
42,
43,
44,
45,
46,
47,
48,
49,
50,
51,
52,
53,
54,
55,
56,
57,
58,
59,
60,
61,
62,
63,
64,
65,
66,
67,
68,
69,
70,
71,
72,
73,
74,
75,
76,
77,
78,
79,
80,
81,
82,
83,
84,
85,
86,
87,
88,
89,
90,
91,
92,
93,
94,
95,
96,
97,
98,
99,
100,
101,
102,
103,
104,
105,
106,
107,
108,
109,
110,
111,
112,
113,
114,
115,
116,
117,
118,
119,
120
],
"mark": [
{
"tenthmark": "345",
"twelthmark": "1100"
},
{
"Diploma": "G",
"DiplomaPG": "R"
},
{
"Ug": "D",
"PG": "E"
}
]
}
并使用我为您创建的以下视图模型
public class Place
{
public string native { get; set; }
public string school { get; set; }
public string college { get; set; }
}
public class Mark
{
public string tenthmark { get; set; }
public string twelthmark { get; set; }
public string Diploma { get; set; }
public string DiplomaPG { get; set; }
public string Ug { get; set; }
public string PG { get; set; }
}
public class RootObject
{
public string name { get; set; }
public Place place { get; set; }
public string dob { get; set; }
public List<int> ids { get; set; }
public List<Mark> mark { get; set; }
}
然后使用
反序列化
RootObject rootObject = JsonConvert.DeserializeObject<RootObject>(data);
不能反序列化为接口,只能反序列化为具体类型,如List of T
您不能只反序列化为 IEnumerable...您需要一个模型来放入数据。您需要一个反映您的 JSON 结构的 ViewModel,然后您可以轻松反序列化为 IEnumerable你的 ViewModel 类型。
JsonConvert.DeserializeObject<IEnumerable<SomeViewModel>>(jsonData)
我有一个 json 对象。我想将它反序列化为 IEnumerable
类型的对象。
我在 asp.net Core 工作。
我被这个试过了:
IEnumerable div = (IEnumerable)JsonConvert.DeserializeObject(value, typeof(IEnumerable));
这里的值是 json 变量。
我遇到了这种类型的异常:
Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.IEnumerable' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Json 文件:
{
"name": "xx",
"place": {
"native": "aa",
"school": "ee",
"college": "dd"
},
"dob": "ss",
"ids": [
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
26,
27,
28,
29,
30,
31,
32,
33,
34,
35,
36,
37,
38,
39,
40,
41,
42,
43,
44,
45,
46,
47,
48,
49,
50,
51,
52,
53,
54,
55,
56,
57,
58,
59,
60,
61,
62,
63,
64,
65,
66,
67,
68,
69,
70,
71,
72,
73,
74,
75,
76,
77,
78,
79,
80,
81,
82,
83,
84,
85,
86,
87,
88,
89,
90,
91,
92,
93,
94,
95,
96,
97,
98,
99,
100,
101,
102,
103,
104,
105,
106,
107,
108,
109,
110,
111,
112,
113,
114,
115,
116,
117,
118,
119,
120
],
"mark": [
{
"10thmark": "345",
"12thmark": "1100"
},
{
"Diploma": "G",
"Diploma PG": "R"
},
{
"Ug": "D",
"PG": "E"
}
]
}
如何解决?
1) 如果你的json是一个对象 例如我的用户
public class MyUser
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
而你的json是
{
"Id": 1,
"Name": "Andrew",
"Description": "Software Engineer"
}
然后你可以使用下面的代码轻松反序列化
MyUser user = JsonConvert.DeserializeObject<MyUser>(data);
上面代码中的"data"是你的json文件数据。
2) 如果你的 json 是一个对象数组 例如MyUser 数组。
{
"Users": [
{ "Id": 1, "Name": "Andrew", "Description": "Software Engineer"},
{ "Id": 2, "Name": "Eddy", "Description": "Software Developer"},
{ "Id": 3, "Name": "Matthew", "Description": "Web Developer"}
]
}
那么你需要一个 IEnumerable 类型的视图模型 属性 MyUser 来反序列化
public class MyUserVM
{
public IEnumerable<MyUser> Users { get; set; }
}
然后你需要使用上面的视图模型反序列化
MyUserVM users = JsonConvert.DeserializeObject<MyUserVM>(data);
上面代码中的"data"是你的json文件数据。
Q) 为什么这里需要视图模型?
A) 因为 json 是基于文本的键值格式数据,您需要一个键才能在 JsonConvert class.
中读取它已编辑:
请像下面这样更改 json 密钥
{
"name": "xx",
"place": {
"native": "aa",
"school": "ee",
"college": "dd"
},
"dob": "ss",
"ids": [
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
26,
27,
28,
29,
30,
31,
32,
33,
34,
35,
36,
37,
38,
39,
40,
41,
42,
43,
44,
45,
46,
47,
48,
49,
50,
51,
52,
53,
54,
55,
56,
57,
58,
59,
60,
61,
62,
63,
64,
65,
66,
67,
68,
69,
70,
71,
72,
73,
74,
75,
76,
77,
78,
79,
80,
81,
82,
83,
84,
85,
86,
87,
88,
89,
90,
91,
92,
93,
94,
95,
96,
97,
98,
99,
100,
101,
102,
103,
104,
105,
106,
107,
108,
109,
110,
111,
112,
113,
114,
115,
116,
117,
118,
119,
120
],
"mark": [
{
"tenthmark": "345",
"twelthmark": "1100"
},
{
"Diploma": "G",
"DiplomaPG": "R"
},
{
"Ug": "D",
"PG": "E"
}
]
}
并使用我为您创建的以下视图模型
public class Place
{
public string native { get; set; }
public string school { get; set; }
public string college { get; set; }
}
public class Mark
{
public string tenthmark { get; set; }
public string twelthmark { get; set; }
public string Diploma { get; set; }
public string DiplomaPG { get; set; }
public string Ug { get; set; }
public string PG { get; set; }
}
public class RootObject
{
public string name { get; set; }
public Place place { get; set; }
public string dob { get; set; }
public List<int> ids { get; set; }
public List<Mark> mark { get; set; }
}
然后使用
反序列化RootObject rootObject = JsonConvert.DeserializeObject<RootObject>(data);
不能反序列化为接口,只能反序列化为具体类型,如List of T
您不能只反序列化为 IEnumerable...您需要一个模型来放入数据。您需要一个反映您的 JSON 结构的 ViewModel,然后您可以轻松反序列化为 IEnumerable你的 ViewModel 类型。
JsonConvert.DeserializeObject<IEnumerable<SomeViewModel>>(jsonData)