Vishal
Vishal

Reputation: 1

How to convert complex json string to object c#

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?

Upvotes: 0

Views: 441

Answers (1)

E. Horgan
E. Horgan

Reputation: 21

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

Upvotes: 2

Related Questions