无法反序列化JSON数组(例如[1,2,3])入式''因为类型需要的JSON对象(例如{"名":"价值"})正确反序列化 [英] Cannot deserialize the JSON array (e.g. [1,2,3]) into type ' ' because type requires JSON object (e.g. {"name":"value"}) to deserialize correctly

查看:4203
本文介绍了无法反序列化JSON数组(例如[1,2,3])入式''因为类型需要的JSON对象(例如{"名":"价值"})正确反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Json如下图所示:

  [
    {
        属性:
            {
                键:姓名,
                值:{
                    值:加1,
                    价值观:
                        加1
                    ]
                }
            },
            {
                键:ID,
                值:{
                    值:1,
                    价值观:
                        1
                    ]
                }
            }
        ]
        名称:账户,
        ID:1
    },
    {
        属性:
            {
                键:姓名,
                值:{
                    值:加2,
                    价值观:
                        ACC 2
                    ]
                }
            },
            {
                键:ID,
                值:{
                    值:2,
                    价值观:
                        2
                    ]
                }
            }
        ]
        名称:账户,
        ID:2
    },
    {
        属性:
            {
                键:姓名,
                值:{
                    值:加3,
                    价值观:
                        加3
                    ]
                }
            },
            {
                键:ID,
                值:{
                    值:3,
                    价值观:
                        3
                    ]
                }
            }
        ]
        名称:账户,
        ID:2
    }
]

对于我有类象下面这样:

 公共类RetrieveMultipleResponse
{
    公开名单<属性与GT;属性{搞定;组; }
    公共字符串名称{;组; }
    公共字符串ID {搞定;组; }
}
公共类值
{
    [JsonProperty(值)
    公共字符串值{获得;组; }
    公开名单<串GT;值{搞定;组; }
}公共类属性
{
    公共字符串键{获得;组; }
    公共值值{搞定;组; }
}

我想用下面code反序列化JSON以上:

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

但我得到下面的错误是:


  

无法反序列化当前的JSON数组(例如[1,2,3])入式
  test.Model.RetrieveMultipleResponse,因为类型需要一个JSON
  对象(例如{名:值})正确地反序列化。为了解决这个问题
  错误要么改变JSON到一个JSON对象(例如{名:值})
  或改变反序列化类型的数组或一个类型实现
  集合接口(如ICollection的,IList的)喜欢的列表,可以
  从JSON数组反序列化。 JsonArrayAttribute也可以是
  加入类型,迫使它从一个JSON数组进行反序列化。路径
  '',1号线,位置1。



解决方案

您JSON字符串方括号( [] )内包装,因此它是跨preTED作为数组,而不是单一的 RetrieveMultipleResponse 对象。因此,你需要反序列化它类型的集合 RetrieveMultipleResponse ,例如:

  VAR objResponse1 =
    JsonConvert.DeserializeObject<名单,LT; RetrieveMultipleResponse>>(JsonStr);

I have Json like below:

[
    {
        "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"
    }
]

for that i Have class like below:

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

I am trying to deserialize the above json using below code:

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

but i am getting below error:

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.

解决方案

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对象(例如{&QUOT;名&QUOT;:&QUOT;价值&QUOT;})正确反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆