Search code examples
c#windows-phone-7windows-phone-8windows-phonejson.net

Handling JSON variable without property


I have next 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"
                    }]
            },

I don't understand what to write for the string property in result:

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

I understand that it is array like Result, but it has different names

I have tried this:

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

But it fails


Solution

  • I agree it's a bit of a crappy way to represent a data structure, but it is still valid json, see enter link description here

    I think the Dictionary type would de-serialize this okay assuming you are using newtonsoft's Json.net .

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