I\'ve got a JSON stream coming back from a server, and I need to search for a specific value of the node \"ID\" using JSON.net to parse the data. And I can almost make it wo
Simply write a recursive function:
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 childFolders { get; set; } = new List();
public List childComponents { get; set; } = new List();
}
And using it:
var obj = JsonConvert.DeserializeObject(jsonString);
var result = FindThing(obj.data, "route3");