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

0

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

4 Comments

I see. Is there a way to tell the parser to allow multiple keys?
The keys can be multiple, but not the same/duplicate
Sorry, I meant duplicate keys. Is there a way to allow them?
I doubt if there is

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.