c# – 无法反序列化JSON数组(例如[1,2,3])到类型”,因为类型需要JSON对象(例如{“name”:“value”})来反序列化

我有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:

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

但我越来越错误:

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的集合,例如:

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

转载注明原文:c# – 无法反序列化JSON数组(例如[1,2,3])到类型”,因为类型需要JSON对象(例如{“name”:“value”})来反序列化 - 代码日志