Click here to Skip to main content
13,894,773 members
Rate this:
 
Please Sign up or sign in to 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 27-Jun-18 10:22am
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)...

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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.
   
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);

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web05 | 2.8.190306.1 | Last Updated 27 Jun 2018
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100