Cannot deserialize the JSON array (e.g. [1,2,3]) into type #39; #39; because type requires JSON object (e.g. {quot;namequot;:quot;valuequot;}) to deserialize correctly(无法将 JSON 数组(例如 [1,2,3])反序列化为类型“,因为类型需要 JSON 对象(例如 {“name:“value})才能正确反序列化) - IT屋
问题描述
我有这个 JSON:
[
{
"Attributes": [
{
"Key": "Name",
"Value": {
"Value": "Acc 1",
"Values": [
"Acc 1"
]
}
},
{
"Key": "Id",
"Value": {
"Value": "1",
"Values": [
"1"
]
}
}
],
"Name": "account",
"Id": "1"
},
{
"Attributes": [
{
"Key": "Name",
"Value": {
"Value": "Acc 2",
"Values": [
"Acc 2"
]
}
},
{
"Key": "Id",
"Value": {
"Value": "2",
"Values": [
"2"
]
}
}
],
"Name": "account",
"Id": "2"
},
{
"Attributes": [
{
"Key": "Name",
"Value": {
"Value": "Acc 3",
"Values": [
"Acc 3"
]
}
},
{
"Key": "Id",
"Value": {
"Value": "3",
"Values": [
"3"
]
}
}
],
"Name": "account",
"Id": "2"
}
]
我有这些课程:
public class RetrieveMultipleResponse
{
public List<Attribute> Attributes { get; set; }
public string Name { get; set; }
public string Id { get; set; }
}
public class Value
{
[JsonProperty("Value")]
public string value { get; set; }
public List<string> Values { get; set; }
}
public class Attribute
{
public string Key { get; set; }
public Value Value { get; set; }
}
我正在尝试使用以下代码反序列化上述 JSON:
I am trying to deserialize the above JSON using the code below:
var objResponse1 = JsonConvert.DeserializeObject<RetrieveMultipleResponse>(JsonStr);
但我收到此错误:
无法将当前 JSON 数组(例如 [1,2,3])反序列化为类型'test.Model.RetrieveMultipleResponse' 因为该类型需要 JSON对象(例如 {"name":"value"})正确反序列化.要解决这个问题错误要么将 JSON 更改为 JSON 对象(例如 {"name":"value"})或将反序列化类型更改为数组或实现的类型一个集合接口(例如 ICollection、IList),比如 List 可以从 JSON 数组反序列化.JsonArrayAttribute 也可以添加到类型以强制它从 JSON 数组反序列化.小路'',第 1 行,位置 1.
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'test.Model.RetrieveMultipleResponse' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path '', line 1, position 1.
推荐答案
您的 json 字符串包含在方括号 ([]) 中,因此它被解释为数组而不是单个 RetrieveMultipleResponse 对象.因此,您需要将其反序列化为 RetrieveMultipleResponse 的类型集合,例如:
Your json string is wrapped within square brackets ([]), hence it is interpreted as array instead of single RetrieveMultipleResponse object. Therefore, you need to deserialize it to type collection of RetrieveMultipleResponse, for example :
var objResponse1 =
JsonConvert.DeserializeObject<List<RetrieveMultipleResponse>>(JsonStr);
这篇关于无法将 JSON 数组(例如 [1,2,3])反序列化为类型“",因为类型需要 JSON 对象(例如 {“name":“value"})才能正确反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:无法将 JSON 数组(例如 [1,2,3])反序列化为类型“",因为类型需要 JSON 对象(例如 {“name":“value"})才能正确反序列化
基础教程推荐
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
- 从 C# 控制相机设备 2022-01-01
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- 获取C#保存对话框的文件路径 2022-01-01
- 如果条件可以为空 2022-01-01
- C# 9 新特性——record的相关总结 2023-04-03
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
- 将数据集转换为列表 2022-01-01
