Click here to Skip to main content
15,884,237 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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


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());
Posted
Updated 18-Sep-22 1:01am
Comments
Kornfeld Eliyahu Peter 27-Jun-18 16:18pm    
To fir your class definition res_data should be an array of objects and not a single object (even there is only one element)...

Your issue is pretty clear.

Quote:
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.

Quote:
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


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.
 
Share this answer
 
v2
Comments
Ajay_Saini 27-Jun-18 20:45pm    
Thanks a lot to point on exact correction i changed the type of res_data and deserialize it by following normal way and it works. I had not noticed that in my Json the res_data have a single object. My bad....

var Data = JsonConvert.DeserializeObject<api.responsedata2>(response);
Json Format is wrong so please correct your Json format after try then is running your code.

Example :-
string response = @"{'api_res_key':'Key45VALID','res_status':'200','res_msg':'REGISTER','res_data':[{ 'OTP':'851446','login_id':'S20180627YB11v'}]}";
 
Share this answer
 
v2
Comments
Richard Deeming 20-Sep-22 8:47am    
As already adequately explained four years ago in the accepted solution. You have added nothing new to the discussion.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900