Finding a nested value inside a JSON.net object in C #

I have a JSON stream coming back from the server and I need to find the specific node "ID" value using JSON.net to analyze the data. And I can almost make it work, but not quite because the returned results are deeply embedded in each other - this is due to the fact that I get the folder structure back. I boiled JSON to a simpler version. I get this:

{
    "data": {
        "id": 0,
        "name": "",
        "childFolders": [{
            "id": 19002,
            "name": "Locker",
            "childFolders": [{
                "id": 19003,
                "name": "Folder1",
                "childFolders": [],
                "childComponents": [{
                    "id": 19005,
                    "name": "route1",
                    "state": "STOPPED",
                    "type": "ROUTE"
                }]
            }, {
                "id": 19004,
                "name": "Folder2",
                "childFolders": [],
                "childComponents": [{
                    "id": 19008,
                    "name": "comm1",
                    "state": "STOPPED",
                    "type": "COMMUNICATION_POINT"
                }, {
                    "id": 19006,
                    "name": "route2",
                    "state": "STOPPED",
                    "type": "ROUTE"
                }, {
                    "id": 19007,
                    "name": "route3",
                    "state": "STOPPED",
                    "type": "ROUTE"
                }]
            }],
            "childComponents": []
        }],
        "childComponents": []
    },
    "error": null
}

I can almost get there by going:

var objects = JObject.Parse(results);
var subobjects = objects["data"]["childFolders"][0]["childFolders"][1];

In the debug view, I see that it will analyze the object, but will not allow me to search inside.

- "route3" 19007, . , , , . , , 2 20 .

+4
3

- "route3" 19007

linq Descendants JObject:

var dirs = JObject.Parse(json)
            .Descendants()
            .Where(x=>x is JObject)
            .Where(x=>x["id"]!=null && x["name"]!=null)
            .Select(x =>new { ID= (int)x["id"], Name = (string)x["name"] })
            .ToList();

var id = dirs.Find(x => x.Name == "route3").ID;
+4

:

private Thing FindThing(Thing thing, string name)
{
    if (thing.name == name)
        return thing;
    foreach (var subThing in thing.childFolders.Concat(thing.childComponents))
    {
        var foundSub = FindThing(subThing, name);
        if (foundSub != null)
            return foundSub;
    }
    return null;
}

class RootObject
{
    public Thing data { get; set; }
}

class Thing
{
    public int id { get; set; }
    public string name { get; set; }
    public List<Thing> childFolders { get; set; } = new List<Thing>();
    public List<Thing> childComponents { get; set; } = new List<Thing>();
}

:

var obj = JsonConvert.DeserializeObject<RootObject>(jsonString);
var result = FindThing(obj.data, "route3");
+3

SelectToken SelectTokens, JPath node. , , :

JObject.Parse(jsonData)["data"].SelectToken("$..childComponents[?(@.name=='route3')]")

JPath

+3
source

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


All Articles