C#无密钥反序列化

时间:2014-06-25 06:54:27

标签: c# json json.net

我正在使用C#来解析返回JSON数据的Web API。

{
    "response": {
        "success": 1,
        "items": {
            "Items 1's name": {
                "index": [
                    1
                ],
                "data": {
                    "1": {
                        "special1": {
                            "special2": [
                                {
                                    "value": "4 usd"
                                }
                            ]
                        }
                    }
                }
            }
       }
    }
}

我已设法使用

解析“成功”部分的“回复”
public class ResponseResult {
        [JsonProperty("response")]
        public bpResponse Response { get; set; }
    }

    public class bpResponse {
        [JsonProperty("success")]
        public string Success { get; set; }

        [JsonProperty("items")]
        public Item items { get; set; }
    }

using (var jr = new JsonTextReader(new StringReader(data))) {
    var js = new JsonSerializer();
    var u = js.Deserialize<ResponseResult>(jr);
    Console.WriteLine(u.Response.Current_Time);
}

在此之后我被困住,因为我意识到对于项目的名称,没有可用的密钥。有人可以指导我如何继续解析数据吗?

提前致谢!

1 个答案:

答案 0 :(得分:0)

假设您拥有有效的JSON,然后您可以使用下一个结构对其进行反序列化:

public class ResponseResult
{
    [JsonProperty("response")]
    public bpResponse Response { get; set; }
}

public class bpResponse
{
    [JsonProperty("success")]
    public string Success { get; set; }

    [JsonProperty("items")]
    public Item Items { get; set; }
}

public class Item
{
    [JsonProperty("Items 1's name")]
    public ItemFirstName ItemFirstName { get; set; }
}

public class ItemFirstName
{
    [JsonProperty("index")]
    public List<string> Index { get; set; }

    [JsonProperty("data")]
    public Data Data { get; set; }
}

public class Data
{
    [JsonProperty("1")]
    public First First { get; set; }
}

public class First
{

    [JsonProperty("special1")]
    public Special1 Special1 { get; set; }
}

public class Special1
{
    [JsonProperty("special2")]
    public List<Special2> Special2 { get; set; }
}

public class Special2
{
    [JsonProperty("value")]
    public string Value { get; set; }
}