Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading