Search code examples
c#asp.netjson.netjsonparser

What is wrong with this deserialization? Keep getting error trying to deserialize the response file


What is wrong with this deserialization of my response file? I Keep getting exception when I am trying to deserialize the response file to my model that I created. When I try to put the response json in a file and run the deserialize it works fine. But when I am trying to convert the result it keeps failing. What am I doing wrong?

HttpResponseMessage response = client.GetAsync(URL+pg.urlParameters).Result;  
if (response.IsSuccessStatusCode)
{

    var result = response.Content.ReadAsStringAsync();

    var detailsList = Newtonsoft.Json.JsonConvert.DeserializeObject<Rootobject>(result.ToString());

I get an exception as Newtonsoft.Json.JsonReaderException: 'Unexpected character encountered while parsing value: S. Path '', line 0, position 0.'

Image of the above JSON


Solution

  • You forgot to put the 'await' keyword. So your result is a Task and not the actual result yet. This should fix it:

    if (response.IsSuccessStatusCode)
        {
    
            var result = await response.Content.ReadAsStringAsync();
    
            var detailsList = Newtonsoft.Json.JsonConvert.DeserializeObject<Rootobject>(result);