CSharp · Console
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
public class Program
{
public static void Main()
{
string json = @"
[
{
""children"": [
{
""children"": [
{
""children"": [],
""text"": ""Child node 2.txt"",
""isFolder"": false,
""id"": ""1childnode2.txt"",
""itsPath"": ""C:\\Users\\pandyapa\\Root node\\Child node 2.txt"",
""type"": ""itsfile""
}
],
""text"": ""Child node 1.txt"",
""isFolder"": false,
""id"": ""0childnode1.txt"",
""itsPath"": ""C:\\Users\\pandyapa\\Root node\\Child node 1.txt"",
""type"": ""itsfile""
},
{
""children"": [],
""text"": ""Child node 2.txt"",
""isFolder"": false,
""id"": ""1childnode2.txt"",
""itsPath"": ""C:\\Users\\pandyapa\\Root node\\Child node 2.txt"",
""type"": ""itsfile""
},
{
""children"": [],
""text"": ""Child node 3.txt"",
""isFolder"": false,
""id"": ""2childnode3.txt"",
""itsPath"": ""C:\\Users\\pandyapa\\Root node\\Child node 3.txt"",
""type"": ""itsfile""
}
],
""text"": ""Root node"",
""isFolder"": true,
""id"": ""3rootnode"",
""itsPath"": ""C:\\Users\\pandyapa\\Root node"",
""type"": ""default""
}
]";
List<Node> nodes = JsonConvert.DeserializeObject<List<Node>>(json);
var idToFind = "2childnode3.txt";
var node = nodes.SelectMany(n => n.DescendantsAndSelf())
.Where(n => n.Id == idToFind)
.FirstOrDefault();
if (node != null)
{
Console.WriteLine("ItsPath: " + node.ItsPath);
}
else
{
Console.WriteLine("Not found");
}
}
}
public class Node
{
public Node[] Children { get; set; }
public string Text { get; set; }
public bool IsFolder { get; set; }
public string Id { get; set; }
public string ItsPath { get; set; }
public string Type { get; set; }
public IEnumerable<Node> DescendantsAndSelf()
{
yield return this;
if (Children != null)
{
foreach (Node child in Children)
{
yield return child;
}
}
}
}