1

When I parse the json string

 var testJson = @"{'entry1': {
                     '49208118': {
                         'name': 'just a name'
                          }
                     },
                     'entry2': {
                       '49208118': [
                          {
                             'description': 'just a description'
                          },
                          {
                             'description': 'another description' 
                          }
                       ]
                     }
                }";

using

dynamic conversionResult = JsonConvert.DeserializeObject(testJson);

I can easily access the first level entries using

Assert.IsNotNull(conversionResult.entry1);
Assert.IsNotNull(conversionResult.entry2);

But how can I access the subentries of each entry?

conversionResult.entry1.49208118.name;

Obviously does not work.

Please note: the string is a simplified version of a json string retrieved from an API and the subentry id '49208118' is completely random and changes each time the API is called.

Oblomov
  • 8,953
  • 22
  • 60
  • 106
  • Take a look at [Cannot access properties after JSON deserialization into dynamic](https://stackoverflow.com/q/24357150/3744182). In fact I'd say your question is a duplicate; agree? – dbc May 14 '18 at 14:44
  • There is one subtle difference: In my case, the id's are completely random, so I cant access the sub-proberties directly. Otherwise thanks: The answer to the other question pretty much answers mine as well. – Oblomov May 14 '18 at 14:54
  • 1
    OK. I closed it off as a duplicate, and linked to a couple of answers showing how to loop through arbitrary properties of a `JObject`, the type Json.NET uses to represent a JSON object. – dbc May 14 '18 at 15:24
  • 1
    [How do I get a deeply-nested property from JSON string?](https://stackoverflow.com/q/36324494/3744182) might also help. – dbc May 14 '18 at 15:36

0 Answers0