0

我有下一个 JSON:

"jsonrpc":"2.0",
"id":1,
"result":
    {
        "1375833600000":
            {
                "notifications":[],
                "events":[],
                "lectures":[],
                "birthdays":[
                    {
                        "id":"BB390BEE-CCBE-4D42-849C-E5040D6FD12C",
                        "date":1375909200000,
                        "name":"Test test",
                        "previewPhoto":"https://link.com/public/photos/h66/BB390BEE-CCBE-4D42-849C-E5040D6FD12C.jpg"
                    }]
            },
         "1375833600001":
            {
                "notifications":[],
                "events":[],
                "lectures":[],
                "birthdays":[
                    {
                        "id":"BB390BEE-CCBE-4D42-849C-E5040D6FD12C",
                        "date":1375909200000,
                        "name":"Test test",
                        "previewPhoto":"https://link.com/public/photos/h66/BB390BEE-CCBE-4D42-849C-E5040D6FD12C.jpg"
                    }]
            },

我不明白在结果中为字符串属性写什么:

class Schedule
{
    [JsonProperty("jsonrpc")]
    public string Jsonrpc { get; set; }
    [JsonProperty("id")]
    public string Id { get; set; }
    [JsonProperty("result")]
    public Result Result { get; set; }
}

public class Result
{
    How to handle this: "1375833600000"
                        "1375833600001"
}

我知道它是像 Result 一样的数组,但它有不同的名称

我试过这个:

class Schedule
{
    [JsonProperty("jsonrpc")]
    public string Jsonrpc { get; set; }
    [JsonProperty("id")]
    public string Id { get; set; }
    private List<Results> result = new List<Results>();
    [JsonProperty("result")]
    public List<Results> Result { get { return result; } }
}

public class Results
{
    private List<Event> events = new List<Event>();
    [JsonProperty("events")]
    public List<Event> Events { get { return events; } }

    private List<Birthday> birthdays = new List<Birthday>();
    [JsonProperty("birthdays")]
    public List<Birthday> Birthdays { get { return birthdays; } }
}

public class Event
{
    [JsonProperty("id")]
    public int Id { get; set; }
    [JsonProperty("datetime")]
    public long Datetime { get; set; }
    [JsonProperty("title")]
    public string Title { get; set; }
    [JsonProperty("link")]
    public string Link { get; set; }
    [JsonProperty("description")]
    public string Description { get; set; }
    [JsonProperty("allDayEvent")]
    public bool AllDayEvent { get; set; }
}

public class Birthday
{
    [JsonProperty("id")]
    public int Id { get; set; }
    [JsonProperty("date")]
    public long Date { get; set; }
    [JsonProperty("name")]
    public string Name { get; set; }
    [JsonProperty("previewPhoto")]
    public string PreviewPhoto { get; set; }
}

但它失败了

4

1 回答 1

1

我同意表示数据结构的方式有点蹩脚,但它仍然是有效的 json,请参阅在此处输入链接描述

假设您使用的是 newtonsoft 的 Json.net ,我认为 Dictionary 类型可以反序列化。

 public class Schedule
{
    [JsonProperty("jsonrpc")]
    public string Jsonrpc { get; set; }
    [JsonProperty("id")]
    public string Id { get; set; }
    [JsonProperty("result")]
    public Dictionary<string, Datas> Result { get; set; }
}

public class Datas
{
    public IEnumerable<string> Notifications { get; set; }
    public IEnumerable<string> Events { get; set; }
    public IEnumerable<string> Lectures { get; set; }

}
于 2013-08-14T09:00:28.547 回答