0

I'm trying to parse a JSON string to a JObject, but somehow it only parses the first object of an Array.

This is a part of a JSON string

        {
          "Categories": [
            {
              "Category": [
                {
                  "ID": "1",
                  "Description": "Kochen/Backen",
                  "IsActive": "True"
                }
              ],
              "Category":[
                {
                  "ID": "2",
                  "Description": "Sport",
                  "IsActive": "True"
                }
              ],
              "Category": [
                {
                  "ID": "3",
                  "Description": "Begleitung 2",
                  "IsActive": "True"
                }
              ]
            }
          ],

And after JObject.Parse, I can see it loaded:

        {
          "Categories": [
            {
              "Category": [
                {
                  "ID": "3",
                  "Description": "Begleitung 2",
                  "IsActive": "True"
                }
              ]
            }
          ],

So why are the first 2 Categories not parsed? I'm not a pro with JSON, so I don't know if the string is correct that way.

Thanks for your help

DirtyNative
  • 2,553
  • 2
  • 33
  • 58

1 Answers1

0

Your JObject cannot hold duplicate keys. The dictionary in the parent list, has multiple entries with same key Category:

{
  "Category": [
    {
      "ID": "1",
      "Description": "Kochen/Backen",
      "IsActive": "True"
    }
  ],
  "Category":[
    {
      "ID": "2",
      "Description": "Sport",
      "IsActive": "True"
    }
  ],
  "Category": [ 
    {
      "ID": "3",
      "Description": "Begleitung 2",
      "IsActive": "True"
    }
  ]
}

So the other keys are overwritten after parsing and the last item with id 3 becomes the final value. Consider restructuring the keys into say Category1, Category2 and Category3

Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139