0

I wanna convert Json String to Object in c#. I tried several times with several codes but its gave me a parsing error. Refer below json.

{
"Test Name1": [{
    "scores": [{
        "score": "-0.00",
        "ethnicity": "Asian"
    },
    {
        "score": "0.00",
        "ethnicity": "GreaterAfrican"
    },
    {
        "score": "1.00",
        "ethnicity": "GreaterEuropean"
    }],
    "best": "European"
},
{
    "scores": [{
        "score": "1.00",
        "ethnicity": "British"
    },
    {
        "score": "0.00",
        "ethnicity": "Jewish"
    },
    {
        "score": "-0.00",
        "ethnicity": "WestEuropean"
    },
    {
        "score": "0.00",
        "ethnicity": "EastEuropean"
    }],
    "best": "British"
}],
"Test Name2": [{
    "scores": [{
        "score": "-0.00",
        "ethnicity": "Asian"
    },
    {
        "score": "0.00",
        "ethnicity": "GreaterAfrican"
    },
    {
        "score": "1.00",
        "ethnicity": "GreaterEuropean"
    }],
    "best": "GreaterEuropean"
},
{
    "scores": [{
        "score": "-5.95",
        "ethnicity": "British"
    },
    {
        "score": "6.95",
        "ethnicity": "Jewish"
    },
    {
        "score": "0.00",
        "ethnicity": "WestEuropean"
    },
    {
        "score": "-0.00",
        "ethnicity": "EastEuropean"
    }],
    "best": "Jewish"
}]

}

I am trying with below code.

var Result = client.PostAsync(APIURL, httpContent).Result;
if(Result.IsSuccessStatusCode)
  {
      var responseStr = Result.Content.ReadAsStringAsync();       
                    dynamic jsonObject = JsonConvert.DeserializeObject<object>(responseStr.ToString());                        
  }

But unable to convert this to object. How it can be done?

6
  • 3
    What's the error message that you receive? Commented Feb 17, 2017 at 21:48
  • Are you using Visual Studio? Commented Feb 17, 2017 at 21:49
  • You must await the async call or you will run the next line before the content is read. Commented Feb 17, 2017 at 21:50
  • In JsonConvert.DeserializeObject<object>, "object" shall be replaced with a class whose public variables correspond to the input Json structure. (high level classes may refer to lower level classes). Commented Feb 17, 2017 at 21:51
  • Also, if you are doing this in an asp.net context don't use .Result on the PostAsync. It can deadlock so just await that as well. Commented Feb 17, 2017 at 21:57

1 Answer 1

2

You are trying to deserialize a "tostring" representation of a task, rather than the return value of that task.

Try this:

if(Result.IsSuccessStatusCode)
{
    var responseStr = await Result.Content.ReadAsStringAsync();       
    dynamic jsonObject = JsonConvert.DeserializeObject<object>(responseStr);                        
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.