I'm trying to parse a JSON response that includes something I'm not quite familiar with, nor have I seen in the wild that often.
Inside one of the JSON objects, there is a dynamically named JSON object.
In this example, there is a JSON object inside "bugs"
named "12345"
which correlates to a bug number.
{
"bugs" : {
"12345" : {
"comments" : [
{
"id" : 1,
"text" : "Description 1"
},
{
"id" : 2,
"text" : "Description 2"
}
]
}
}
}
What I'm curious about is: What would be the most effective way to parse a dynamically-named JSON object like this?
Given some JSON Utility tools like
They will take a JSON response like the one above and morph it into classes like the following respectfully:
jsonutils
public class Comment
{
public int id { get; set; }
public string text { get; set; }
}
public class 12345
{
public IList<Comment> comments { get; set; }
}
public class Bugs
{
public 12345 12345 { get; set; }
}
public class Root
{
public Bugs bugs { get; set; }
}
json2charp
public class Comment
{
public int id { get; set; }
public string text { get; set; }
}
public class __invalid_type__12345
{
public List<Comment> comments { get; set; }
}
public class Bugs
{
public __invalid_type__12345 __invalid_name__12345 { get; set; }
}
public class RootObject
{
public Bugs bugs { get; set; }
}
The problem about this is that it generates a class
with a dynamic name. Thus subsequent queries with other identifiers to this API would result in a failure because the name does not match up nor would a generated [JsonProperty("")]
as it would contain the dynamic class name as per the generated examples above.
Although the JSON is valid, this seems to be a limitation with JSON that is formatted this way. Unfortunately I do not have any control on this JSON API, so I'm curious what the best way to approach this problem would be?