Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用JsonPath从JSON数组中选择项,其中项不包含特定属性_C#_Json.net_Jsonpath - Fatal编程技术网

C# 使用JsonPath从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并尝试使用

我有一个类似于以下内容的JSON文件结构:

{
    "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)]");
我试过使用
如下:
$.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"));