Newtonsoft json反序列化问题 [英] Newtonsoft json deserialisation issue

查看:578
本文介绍了Newtonsoft json反序列化问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试反序列化我的一个Json字符串,但是收到错误 -



我的Json字符串



Hi,
I am trying to deserialisation one of my Json string as below but getting error -

My Json String

string response = @"{'api_res_key':'Key45VALID','res_status':'200','res_msg':'REGISTER','res_data':{ 'OTP':'851446','login_id':'S20180627YB11v'}}";







我得到的错误






The error that i get

Unhandled Exception:

Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Mahendras.API.response]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path 'res_data.OTP', line 1, position 89. occurred





我尝试过:



我在C#中创建了以下属性 -





What I have tried:

I had created following properties in C# -

public class ResponseData2
    {
        public string api_res_key { get; set; }
        public string res_status { get; set; }
        public string res_msg { get; set; }
        public List<response> res_data { get; set; }
    }

    public class response
    {
        public string OTP { get; set; }
        public string login_id { get; set; }
    }







然后我试图从下面的代码中反序列化 -






Then i tried to deserialise from below code -

ResponseData2 Data = JsonConvert.DeserializeObject<ResponseData2>(response);





然后我尝试如下但没有成功





Then i tried as below but not succeeded

var Data = JsonConvert.DeserializeObject<List<API.ResponseData2>>(response);





你能帮我解决一下我做错了什么吗?





我可以用下面的单个键解析它,但我想一次性解析整个对象。





Can you please help me what wrong i am doing?


One more thing i am able to parse it by individual key as below, but i wanted to parse the whole object in one go.

var objResponse = JsonConvert.DeserializeObject<API.ResponseData>(jsonResponse["res_data"].ToString());

推荐答案

你的问题非常清楚。



Your issue is pretty clear.

引用:

无法将当前的JSON对象(例如{name:value})反序列化为输入'System.Collections.Generic.List`1 [Mahendras.API.response]'

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Mahendras.API.response]'





它甚至告诉你你需要做什么修复它。





And it even tells you what you need to do to fix it.

引用:

要修复此错误,请将JSON更改为一个JSON数组(例如[1,2,3])或更改反序列化类型以使其正常

To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal





您的班级表示可以有很多回复(列表或数组)但您提供的json示例将响应作为对象。这就是为什么你不能反序列化json对象列表,因为你的json不是列表的格式。



更改 public List< ;响应> res_data {get;组; } 公共响应res_data {get;组; } 并且你应该可以反序列化你的json字符串。



Your class is saying that there can be many responses (a list or an array) but your json example you've provided has the response as an object. This is why you are getting cannot deserialize json object to list because your json isn't in the format of a list.

Change public List<response> res_data { get; set; } to be public response res_data { get; set; } and you should be able to deserialize your json string.


这篇关于Newtonsoft json反序列化问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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