如何将复杂的json字符串转换为对象c#

时间:2017-02-17 21:47:39

标签: c# json

我想在c#中将Json String转换为Object。我用几个代码尝试了几次,但它给了我一个解析错误。请参阅下面的json。

pyspark.sql.Column

}

我正在尝试下面的代码。

{
"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"
}]

但无法将此转换为对象。怎么做?

1 个答案:

答案 0 :(得分:2)

您正在尝试反序列化任务的“tostring”表示,而不是该任务的返回值。

试试这个:

if(Result.IsSuccessStatusCode)
{
    var responseStr = await Result.Content.ReadAsStringAsync();       
    dynamic jsonObject = JsonConvert.DeserializeObject<object>(responseStr);                        
}