无法将JSON数组(例如[1,2,3])反序列化为类型''因为类型需要JSON对象(例如{"name":"value"})才能正确反序列化

Avi*_*ash 78 c# arrays serialization json

我有这个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"
    }
]
Run Code Online (Sandbox Code Playgroud)

我有这些课程:

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; }
}
Run Code Online (Sandbox Code Playgroud)

我试图使用下面的代码反序列化上面的JSON:

var objResponse1 = JsonConvert.DeserializeObject<RetrieveMultipleResponse>(JsonStr);
Run Code Online (Sandbox Code Playgroud)

但我收到这个错误:

无法将当前JSON数组(例如[1,2,3])反序列化为类型'test.Model.RetrieveMultipleResponse',因为该类型需要JSON对象(例如{"name":"value"})才能正确反序列化.要修复此错误,请将JSON更改为JSON对象(例如{"name":"value"})或将反序列化类型更改为数组或实现集合接口的类型(例如ICollection,IList),例如List从JSON数组反序列化.JsonArrayAttribute也可以添加到类型中以强制它从JSON数组反序列化.路径'',第1行,第1位.

har*_*r07 134

您的json字符串包含在方括号([])中,因此它被解释为数组而不是单个RetrieveMultipleResponse对象.因此,您需要将其反序列化为类型集合RetrieveMultipleResponse,例如:

var objResponse1 = 
    JsonConvert.DeserializeObject<List<RetrieveMultipleResponse>>(JsonStr);
Run Code Online (Sandbox Code Playgroud)

  • 仅供参考,这对我来说不起作用,因为我从 API 获取 JSON,并且一整天都弄错了该死的 URL。&gt;&lt; (6认同)
  • 这里的 RetrieveMultipleResponse 是什么? (3认同)
  • 如果你想把它作为一个单一的对象而不是一个集合,你可以只做 JsonStr.Replace("[","").Replace("]","") 或者这不是一个好习惯吗? (2认同)

Joh*_*ers 10

如果一个人想支持泛型(在扩展方法中)这就是模式......

public  static List<T> Deserialize<T>(this string SerializedJSONString)
{
    var stuff = JsonConvert.DeserializeObject<List<T>>(SerializedJSONString);
    return stuff;
}
Run Code Online (Sandbox Code Playgroud)

它是这样使用的:

var rc = new MyHttpClient(URL);
//This response is the JSON Array (see posts above)
var response = rc.SendRequest();
var data = response.Deserialize<MyClassType>();
Run Code Online (Sandbox Code Playgroud)

MyClassType看起来像这样(必须匹配JSON数组的名称值对)

[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
 public class MyClassType
 {
    [JsonProperty(PropertyName = "Id")]
    public string Id { get; set; }

    [JsonProperty(PropertyName = "Name")]
    public string Name { get; set; }

    [JsonProperty(PropertyName = "Description")]
    public string Description { get; set; }

    [JsonProperty(PropertyName = "Manager")]
    public string Manager { get; set; }

    [JsonProperty(PropertyName = "LastUpdate")]
    public DateTime LastUpdate { get; set; }
 }
Run Code Online (Sandbox Code Playgroud)

使用NUGET下载Newtonsoft.Json在需要的地方添加引用...

using Newtonsoft.Json;
Run Code Online (Sandbox Code Playgroud)


小智 5

无法向解决方案添加评论,但这对我不起作用。对我有用的解决方案是使用:

var des = (MyClass)Newtonsoft.Json.JsonConvert.DeserializeObject(response, typeof(MyClass)); 
return des.data.Count.ToString();
Run Code Online (Sandbox Code Playgroud)

将 JSON 数组反序列化为强类型 .NET 对象