如何从指定级别获得JSON节点



我有这个json:

 {  
   "treeview":[  
      {  
         "text":"blah",
         "nodes":[  
         ]
      },
      {  
         "text":"blah",
         "nodes":[  
         ]
      },
      {  
         "text":"blah",
         "nodes":[  
            {  
               "text":"blah",
               "nodes":[  
                  {  
                     "text":"foo",
                     "nodes":[  
                         // I need to put data in here !!!
                     ]
                  }
               ]
            }
         ]
      },
      {  
         "text":"blah",
         "nodes":[  
         ]
      },
      {  
         "text":"foo",
         "nodes":[  
             // Not here !
         ]
      }
   ]
}

我需要在"节点"元素上添加价值,其中我在级别2 >" equal" equal" foo"。

这是我到目前为止尝试的:

var json = myJson;
// First approach
var selector = (JArray)json.SelectTokens($"$..treeview[?(@.text == 'foo')]");
// Second approach
var selector2 = (JArray)json.SelectToken($"$.treeview[?(@...text == 'foo')]");
selector.Add(new JObject(new JProperty("text", "myValue"));

我不明白查询中的"点"是如何工作的...我只知道当您键入2" dot"时,它会浏览整个JSON ...是否有一种方法可以查询特定的缩进级别?

我弄清楚了,是的,当我们想在文本平原中查询JSON时,我们可以指定一个级别的缩进,这是:

var json = myJson;
var selector = (JArray)json.SelectTokens($"$.treeview[*].nodes[*].nodes[(@.text =='foo')].nodes");
selector.Add(new JObject(new JProperty("text", "myValue")));

您可以在这里进行测试: http://jsonpath.com/

在JSON部分中复制我的JSON示例,然后在JSONPATH中添加:$.treeview[*].nodes[*].nodes[*].text

这就是您可以在所需的"level of identation"中获得'foo'值,而无需指定数组中的任何索引,只需使用此'*'而不是int

您需要指定"节点"数组的正确路径。尝试以下操作:

JObject json = JObject.Parse("{"treeview":[{"text":"blah","nodes":[]},{"text":"blah","nodes":[]},{"text":"blah","nodes":[{"text":"blah","nodes":[{"text":"foo","nodes":[]}]}]},{"text":"blah","nodes":[]},{"text":"foo","nodes":[]}]}");
JArray array = (JArray)json.SelectToken("treeview[2].nodes[0].nodes[0].nodes");
array.Add(new JObject(new JProperty("text", "myValue")));

使用对象工作要比普通的JSON文本容易得多...

使用newtonsoft.json软件包...

class Program
{
    static void Main(string[] args)
    {
        var jsonstring = "{"text":"blah","nodes":[{"text":"foo", "nodes": []}, {"text":"bar", "nodes": []}, {"text":"foo", "nodes": []}]}";
        //This is the root node
        var firstLevelNodes = JsonConvert.DeserializeObject<Node>(jsonstring);
        //All the nodes in the root nodes node collection
        var secondLevelNodes = firstLevelNodes.nodes;
        //All of the nodes in the collections of the second level nodes
        var thirdLevelNodes = secondLevelNodes.SelectMany(sln => sln.nodes);

        Console.WriteLine("First Level Nodes: n" + JsonConvert.SerializeObject(firstLevelNodes).PrettyPrint());
        Console.WriteLine();
        Console.WriteLine("Second Level Nodes: n" + JsonConvert.SerializeObject(secondLevelNodes).PrettyPrint());
        Console.WriteLine();
        Console.WriteLine("Third Level Nodes: n" + JsonConvert.SerializeObject(thirdLevelNodes).PrettyPrint());
        secondLevelNodes.First().nodes = new List<Node> { new Node { text = "new node" , nodes = new List<Node>() } };
        Console.WriteLine();
        Console.WriteLine("Third Level Nodes (with new node): n" + JsonConvert.SerializeObject(thirdLevelNodes).PrettyPrint());
        Console.ReadLine();
    }
}
public static class JSONExtensions
{
    public static string PrettyPrint(this string json)
    {
        dynamic parsedJson = JsonConvert.DeserializeObject(json);
        return JsonConvert.SerializeObject(parsedJson, Formatting.Indented);
    }
}

[Serializable]
public class Node
{
    public string text { get; set; }
    public IEnumerable<Node> nodes { get; set; }
}

输出:

First Level Nodes:
{
  "text": "blah",
  "nodes": [
    {
      "text": "foo",
      "nodes": []
    },
    {
      "text": "bar",
      "nodes": []
    },
    {
      "text": "foo",
      "nodes": []
    }
  ]
}
Second Level Nodes:
[
  {
    "text": "foo",
    "nodes": []
  },
  {
    "text": "bar",
    "nodes": []
  },
  {
    "text": "foo",
    "nodes": []
  }
]
Third Level Nodes:
[]
Third Level Nodes (with new node):
[
  {
    "text": "new node",
    "nodes": []
  }
]

编辑:

因此,如果您只想要使用文本foo的第二级节点。

var secondLevelFooNodes = secondLevelNodes.Where(sln=>sln.text == "foo");
//then use these nodes

edit2:

使用您的实际JSON对象也需要一个TreeView类...

class Program
{
    static void Main(string[] args)
    {
        var jsonstring = "{"treeview":[{"text":"blah","nodes":[]},{"text":"blah","nodes":[]},{"text":"blah","nodes":[{"text":"blah","nodes":[{"text":"foo","nodes":[]}]}]},{"text":"blah","nodes":[]},{"text":"foo","nodes":[]}]}";
        //This is the root node
        var treeView = JsonConvert.DeserializeObject<TreeView>(jsonstring);
        //All the nodes in the root nodes node collection
        var firstLevelNodes = treeView.treeview;
        //All of the nodes in the collections of the first level nodes
        var secondLevelNodes = firstLevelNodes.SelectMany(fln => fln.nodes);
        //All of the nodes in the collections of the second level nodes
        var thirdLevelNodes = secondLevelNodes.SelectMany(sln => sln.nodes);
        Console.WriteLine("The TreeView: n" + JsonConvert.SerializeObject(treeView, Formatting.Indented));
        thirdLevelNodes.First(sln => sln.text == "foo").nodes = new List<Node> { new Node { text = "new node", nodes = new List<Node>() } };
        Console.WriteLine();
        Console.WriteLine("The TreeView (with new node): n" + JsonConvert.SerializeObject(treeView, Formatting.Indented));
        Console.ReadLine();
    }
}
[Serializable]
public class Node
{
    public string text { get; set; }
    public IEnumerable<Node> nodes { get; set; }
}
[Serializable]
public class TreeView
{
    public IEnumerable<Node> treeview { get; set; }
}

输出:

The TreeView:
{
  "treeview": [
    {
      "text": "blah",
      "nodes": []
    },
    {
      "text": "blah",
      "nodes": []
    },
    {
      "text": "blah",
      "nodes": [
        {
          "text": "blah",
          "nodes": [
            {
              "text": "foo",
              "nodes": []
            }
          ]
        }
      ]
    },
    {
      "text": "blah",
      "nodes": []
    },
    {
      "text": "foo",
      "nodes": []
    }
  ]
}
The TreeView (with new node):
{
  "treeview": [
    {
      "text": "blah",
      "nodes": []
    },
    {
      "text": "blah",
      "nodes": []
    },
    {
      "text": "blah",
      "nodes": [
        {
          "text": "blah",
          "nodes": [
            {
              "text": "foo",
              "nodes": [
                {
                  "text": "new node",
                  "nodes": []
                }
              ]
            }
          ]
        }
      ]
    },
    {
      "text": "blah",
      "nodes": []
    },
    {
      "text": "foo",
      "nodes": []
    }
  ]
}

相关内容

  • 没有找到相关文章

最新更新