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


99

我有这个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);

但我收到此错误:

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

Answers:


160

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

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

我必须在“ [”之前删除“ //”,然后它对我有用。谢谢
花哨的

1
如果您想将其保留为单个对象而不是集合,可以只执行JsonStr.Replace(“ [”,“”)。Replace(“]”,“”),还是不是一个好习惯?
Rich

仅供参考,这对我来说不起作用,因为我从API中获取了JSON,并且整整一整天都在抓狂URL。> <
w00ngy

1
什么是RetrieveMultipleResponse?
奇怪的是

谢谢,这对我有用。var objResponse1 = JsonConvert.DeserializeObject <List <QuestionData >>(srt); // myWord = myQuestionData.Word; Debug.Log(“ myWord” + objResponse1 [0] .Word);
StackBuddy

10

如果要支持泛型(在扩展方法中),这就是模式...

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

它的用法如下:

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

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

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

using Newtonsoft.Json;



0

使用这个,FrontData是JSON字符串:

var objResponse1 = JsonConvert.DeserializeObject<List<DataTransfer>>(FrontData);  

并提取清单:

var a = objResponse1[0];
var b = a.CustomerData;
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.