Tho*_*mas 1 c# json json.net jsonpath
我有这个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 !
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
我需要将值放在我位于第2级的 “节点”元素上,并且“文本”等于“ 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"));
Run Code Online (Sandbox Code Playgroud)
我不了解查询中的“点”是如何工作的……我只知道当您键入2个“点”时,它会浏览整个JSON ...是否有只查询特定缩进级别的方法?
我想通了,是的,当我们要查询纯文本格式的json时,我们可以指定级别缩进,方法如下:
var json = myJson;
var selector = (JArray)json.SelectTokens($"$.treeview[*].nodes[*].nodes[(@.text =='foo')].nodes");
selector.Add(new JObject(new JProperty("text", "myValue")));
Run Code Online (Sandbox Code Playgroud)
您可以在这里进行测试: http : //jsonpath.com/
复制过去的json示例中的json示例,并将其添加到jsonPath中: $.treeview[*].nodes[*].nodes[*].text
这样就可以'foo'在"level of identation"不指定数组索引的情况下获取所需的值,只需使用它'*'代替int
| 归档时间: |
|
| 查看次数: |
1545 次 |
| 最近记录: |