What is the most efficient way to parse JSON objects that are dynamically called?

I am trying to parse a JSON response that includes something that I am not completely familiar with, and I have not seen in the wild that often.

Inside one of the JSON objects there is a JSON object with a dynamic name.

In this example, there is a JSON object inside "bugs"with a name "12345"that correlates with the error number.

{
   "bugs" : {
      "12345" : {
         "comments" : [
            {
               "id" : 1,
               "text" : "Description 1"
            },
            {
               "id" : 2,
               "text" : "Description 2"
            }
         ]
      }
   }
}

I'm wondering: What would be the most efficient way to parse a JSON object with a dynamic name?

For some JSON Utility tools, such as

They will take the JSON response as the one above and convert it to classes as follows:

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; }
}

, class . , API , [JsonProperty("")], .

, JSON , JSON, . , JSON API, , ?

+4
2

Newtonsoft.Json JsonConvert Dictionary<String, Comments> :

public class Comment
{
    public int id { get; set; }
    public string text { get; set; }
}

public class Comments
{
    public List<Comment> comments { get; set; }
}

public class RootObject
{
    public Dictionary<String, Comments> bugs { get; set; }
}

:

var json = "{\r\n   \"bugs\" : {\r\n      \"12345\" : {\r\n         \"comments\" : [\r\n            {\r\n               \"id\" : 1,\r\n               \"text\" : \"Description 1\"\r\n            },\r\n            {\r\n               \"id\" : 2,\r\n               \"text\" : \"Description 2\"\r\n            }\r\n         ]\r\n      }\r\n   }\r\n}";

Console.WriteLine(json);

var obj = JsonConvert.DeserializeObject<RootObject>(json);

Console.WriteLine(obj.bugs["12345"].comments.First().text);
+2

Json.NET, Nuget (Newtonsoft.Json) http://www.newtonsoft.com/json.

Json.NET / , , . JObject JToken , Json dev time.

json- .

// load file into a JObject
JObject document;
using (var fileStream = File.OpenRead(someFilePath))
using (var streamReader = new StreamReader(fileStream))
using (var jsonReader = new JsonTextReader(streamReader))
    document = JObject.Load(jsonReader);

// read the JObject
var bugs = (JObject) document["bugs"];
foreach (var bugEntry in bugs)
{
    var bugID = bugEntry.Key;
    var bugData = (JObject) bugEntry.Value;
    var comments = (JArray) bugData["comments"];
    foreach (JObject comment in comments)
        Debug.Print(comment["text"]);
}
+4

Source: https://habr.com/ru/post/1666297/


All Articles