将带有空格的 JSON 属性解析为对象

djn*_*jnz 3 json json.net

我正在使用返回 JSON 的第三方系统。

我正在尝试弄清楚如何反序列化以下 json;

{"getResponse": {
    "Results": {
        "Result 1": {"Row": [{Name:Somename}]
     }
}
Run Code Online (Sandbox Code Playgroud)

我正在使用 Newtonsoft JSON 库。有人知道我如何将其解析为 .Net 对象吗?

Bri*_*ers 5

要将 JSON 解析为对象,JsonConvert.DeserializeObject<T>可以使类结构如下所示:

public class RootObject
{
    public GetResponse getResponse { get; set; }
}

public class GetResponse
{
    public Results Results { get; set; }
}

public class Results
{
    [JsonProperty("Result 1")]
    public Result1 Result1 { get; set; }
}

public class Result1
{
    [JsonProperty("Row")]
    public List<Row> Rows { get; set; }
}

public class Row
{
    public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

然后像这样反序列化:

string json = @"
{
    ""getResponse"": {
        ""Results"": {
            ""Result 1"": {
                ""Row"": [
                    {
                        ""Name"": ""Somename""
                    }
                ]
            }
        }
    }
}";

RootObject root = JsonConvert.DeserializeObject<RootObject>(json);
foreach (Row row in root.getResponse.Results.Result1.Rows)
{
    Console.WriteLine(row.Name);
}
Run Code Online (Sandbox Code Playgroud)