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