C# 使用JsonPath从JSON数组中选择项,其中项不包含特定属性
我有一个类似于以下内容的JSON文件结构:C# 使用JsonPath从JSON数组中选择项,其中项不包含特定属性,c#,json.net,jsonpath,C#,Json.net,Jsonpath,我有一个类似于以下内容的JSON文件结构: { "property1": 1, "property2": 2, "someArray": [ { "item1": 1, "item2": 2 }, { "item1": 5 } ] } 我只想从不包含item2属性的数组中选择对象。我正在使用NewtonsoftJson并尝试使用
{
"property1": 1,
"property2": 2,
"someArray": [
{
"item1": 1,
"item2": 2
},
{
"item1": 5
}
]
}
我只想从不包含item2
属性的数组中选择对象。我正在使用NewtonsoftJson
并尝试使用JSON路径来完成这项工作
我设法选择了确实包含item2
属性的对象,但我不知道反转逻辑的语法
// Selects all the tokens where item2 property exists.
var tokens = jsonToken.SelectTokens("$.someArray[?(@.item2)]");
我试过使用过滤器部分中的code>如下:$.someArray[?(!@.item2)
,但它会抛出一个错误,指出!
是意外字符
在这里,我应该如何反转select逻辑
工作示例。提出了这种方法。希望能有所帮助
我不知道如何使用JsonPath
您可以使用JToken树上的LINQ来执行所需的筛选。
这就是JSON.Net在执行JSONPath查询时在后台使用的内容
我将这样编写linq查询:
(jsonToken["someArray"] as JArray)
.OfType<JObject>()
.Where(x => !x.ContainsKey("item2"));
(jsonToken[“someArray”]作为JArray)
第()类
其中(x=>!x.ContainsKey(“第2项”);
item1:5
(jsonToken["someArray"] as JArray)
.OfType<JObject>()
.Where(x => !x.ContainsKey("item2"));