0

This was a hard question to write the title for.

I have some JSON that I have to work with from an external API. It has a list of categories and subcategories to capture peoples email preferences.

The problem I have is that the categories don't have a 'Title' field and a 'List of Subcategories' field as I would have expected. The Category is just a named wrapper:

 "email_preferences": [
        {
            "Entertainment/Arts": [
                {
                    "value": "Art & exhibitions",
                    "key": "artExhibitions"
                },
                {
                    "value": "Theatre",
                    "key": "theatre"
                }
            ]
        },
        {
            "Lifestyle": [
                {
                    "value": "Health & fitness",
                    "key": "healthFitness"
                },
                {
                    "value": "Gardening",
                    "key": "gardening"
                }
            ]
        } .... etc etc

If I paste this into an online C# model generator it hard codes all the categories as child lists of the email prefs node.

Is there a way to wrangle JSON.Net to allow me to have a dynamic list of categories, rather than hard coded?

EDIT:

This wasn't a duplicate of the referenced question. That one has a series of kvp objects, not a list of them.

There is a duplicate question however here: JSON array to C# Dictionary

BUT the answer didn't work for me as JSON.Net created KVPs with null fields, probably due to not knowing how to use the Create() method.

I ended up having to use a list of single element dictionaries.

Ryan
  • 2,109
  • 1
  • 15
  • 19

0 Answers0