0

I have a JSON string like this:

{
    "entries": {
        "101": [
            { "title": "A" },
            { "title": "B" }
        ],
        "102": [
            ...
        ],
        ... // other properties
    },
    ...
}

One property (entries) is an object with several properties. "entries"' property names are actually some kind of category code and will change depending on the query.

I need to deserialize this JSON string. Deserializing to a class (Newtonsoft.Json.JsonConvert.DeserializeObject<MyClass>(theString)) is out of the question because of the changing property names. So looks like I have to parse it.

The best way that I could think of is to cast "entries" into JObject, get its Properties, and loop through the properties to proceed further. Something like this:

var parsed = JObject.Parse(jsonStr);

var flattened = ((JObject)parsed["entries"])
    .Properties()
    .SelectMany(p => parsed["entries"][p.Name])
    .ToList();

Does this JSON set up (using object properties as some kind of dictionary) have a name? Is there a more straightforward / efficient way to process it with json.net?

Endy Tjahjono
  • 24,120
  • 23
  • 83
  • 123

0 Answers0