2

This is the first time I encountured this difficulty. I'm trying to parse a simple json with c# using newtonsoft.net but I don't know how to handle this kind of problem. My json is in this format :

    {
      "events": {
        "119327": {
          "name": "John doe",
          "start": "1524435387600",
          "end": "152446565612800",
          "details": "description"
          "image_url": "http://go/to/url"
        },
        "119217": {
          "name": "John doe",
          "start": "1524534243387600",
          "end": "15244656612800",
          "details": "description"
          "image_url": "http://go/to/url"
        },
        "14397": {
          "name": "John doe",
          "start": "1524386567600",
          "end": "15244143242800",
          "details": "description"
          "image_url": "http://go/to/url"
        }
      }
    }  

Now as you can see the id act like an object and also as the id itself which I will need it later. Can anyone explain to me on how I can get around to this problem. Any approachments? Any help would be highly appreciated. Thanks in advance.

dbc
  • 104,963
  • 20
  • 228
  • 340
Idev Dev
  • 184
  • 17
  • Looks like a duplicate of [How can I parse a JSON string that would cause illegal C# identifiers?](https://stackoverflow.com/a/24536564/3744182) or [Create a strongly typed c# object from json object with ID as the name](https://stackoverflow.com/a/34213724/3744182). See [How to auto-generate a C# class file from a JSON object string](https://stackoverflow.com/q/21611674/3744182) for instructions on how to generate a model for the individual objects. – dbc Apr 17 '18 at 18:13

1 Answers1

2

I would define some "Event" class like this with

      name
      start
      end
      detail
      image_url

Then, I would try to deserialize events as a Dictionary<int, Event>.

After that, you can always map this to another EventWithId class similar to the Event class with an additional id field, using some LINQ query on the resulting Dictionary.

Pac0
  • 21,465
  • 8
  • 65
  • 74