Reputation: 9635
Given the json string
var testJson = @"{'entry1': {
'49208118': [
{
'description': 'just a description'
},
{
'description': 'another description'
}
],
'29439559': [
{
'description': 'just a description'
},
{
'description': 'another description'
}
]
}
}";
The array value of the key 49208118
can be retrieved by
var root = JToken.Parse(testJson);
var descriptions = root.SelectTokens("..49208118[*]").ToList();
according to this answer.
But how can the whole substructure under entry1
be parsed into a dictionary
Dictionary<string, JArray> descriptions;
mapping the numerical ids to arrays of JObject
s?
Upvotes: 4
Views: 346
Reputation: 101483
Since question is how you can parse entry1
to Dictionary<string, JArray>
- easiest option is:
JToken root = JToken.Parse(testJson);
Dictionary<string, JArray> descriptions = root["entry1"].ToObject<Dictionary<string, JArray>>();
Json.NET allows mixing of .NET classes (Dictionary
) and his own classes (JArray
) without problems when parsing.
Upvotes: 1
Reputation: 20363
How about this:
string selector = String.Format("..{0}[*]", yourKey);
var descriptions = root.SelectTokens(selector).ToList();
Upvotes: 1