0

I'm trying to parse a JSON in C# with Newtonsoft.Json component. The JSON I get is this:

[
      {
        "id": "2",
        "title": "First Title",
        "image": "550x346_442.jpg",
        "audio": null,
        "order": "3",
        "schedule": {
          "4": [
            "17:00",
            "17:30"
          ]
        }
      },
      {
        "id": "3",
        "title": "Second Title",
        "image": "myImage.jpg",
        "audio": null,
        "order": "4",
        "schedule": {
          "4": [
            "17:00",
            "18:00",
            "19:30"
          ],
          "6": [
            "17:30",
            "21:30"
          ]
        }
      },
    ]

I have a class for it and everything BUT schedule parse OK. I don't know what type use with the schedule field. I tried string[], string[,] and string[][] and always got an error. Also tried just string type, and then try to re-parse, but also failed. Every other field is parsed into a string type and works fine. I do the parsing in this way:

MyClass[] myObjects = JsonConvert.DeserializeObject<MyClass[]>(jsonStr);

What type should I use for schedule?

David TG
  • 85
  • 3
  • 10
  • 33
  • You can declare `schedule` as `public Dictionary> schedule { get; set; }` as shown in [Deserializing JSON when key values are unknown](https://stackoverflow.com/a/24901245/3744182) or [Deserializing JSON with unknown object names](https://stackoverflow.com/q/38688570/3744182) or [Create a strongly typed c# object from json object with ID as the name](https://stackoverflow.com/a/34213724/3744182) or [Parsing JSON Object with variable properties into strongly typed object](https://stackoverflow.com/q/34202496/3744182). In fact I think this is a duplicate, agree? – dbc Mar 07 '19 at 20:30
  • I don't think that the question is duplicated (may be it is and I can't see it) but the answer it seems to fit. Let me try it in depth to get a full answer! Thanks! – David TG Mar 07 '19 at 20:46
  • The right answer was public Dictionary, pretty close to what you suggested! Also your solution works but string[] it's more accurate. If you want you can write it down as an answer and I'll chose it as the right one, because it's 98% what you said! :) – David TG Mar 08 '19 at 14:43

1 Answers1

0

Correct type for schedule is:

Dictionary<string, string[]>
David TG
  • 85
  • 3
  • 10
  • 33