我有一个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"
    }
]

And I have these classes:

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; }
}

我正在try 使用以下代码反序列化上述JSON:

var objResponse1 = JsonConvert.DeserializeObject<RetrieveMultipleResponse>(JsonStr);

但是我得到了这个错误:

无法将当前JSON数组(例如[1,2,3])反序列化为类型

推荐答案

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相关问答推荐

使用Jolt将字符串数组转换为JSON对象数组

无法从JSON解析ZonedDateTime,但可以使用格式化程序很好地解析

如何用JQ打印JSON文件的路径和键值

使用jq过滤复杂json对象中的数据

如何避免解析 ISuperObject 类型字段中的 json 对象

当并非所有子对象都有 Select 器字段时 Select

如何在 terraform 输出中打印一组用户信息

使用 map_values Select 包含空格的字符串未按预期工作

打印与 JSON 和 PowerShell 中的模式匹配的子项的父项名称

将 JSON 文件放在哪里以在 Angular 8 应用程序中加载静态 JSON 数据?

如何实现一个通用的 serde_json::from_str

apple-app-site-association json 文件是否会在应用程序中更新?

什么 Python 框架用于没有前端的 REST/JSON Web 服务?

将 JSON 对象推送到 localStorage 中的数组

如何按键查找特定的 JSON 值?

在 JSON.NET 中序列化派生类时的字段顺序

有没有办法折叠 Postman 中的所有 json 字段

如何从 MySQL 中检索 JSON 数据?

将 JsonArray 添加到 JsonObject

在播放框架 JsObject 中解析 Json 数组