1

在下面的 JSON 中,我试图找到一个条件。只有当 OrderNumber 不为空或为空时,我才想要结果。

我试过的条件。但对我没有用的是:

> $..Item[?(@.OrderNumber)]
> $..Item[?(@.CurrentOrder.OrderNumber)]

任何建议将不胜感激。我在这里测试我的查询https://jsonpath.curiousconcept.com/

{
        "Response": {
            "ID": "123456",
             "Items": {
                "Item": [
                    {                       
                        "CurrentOrder": {
                            "OrderNumber": "123",
                            "Status": ""
                        }
                    }
                ]
            }
        }
    }
4

1 回答 1

1

$..Item[?(@.CurrentOrder.OrderNumber)]是适合您情况的正确 JSONPath。

已知您使用的工具无法正常工作。例如看这个问题

我用JSONPath Online Evaluator和 JSON测试了上面的查询

{
    "Response": {
        "ID": "123456",
        "Items": {
            "Item": [
                {
                    "CurrentOrder": {
                        "OrderNumber": "123",
                        "Status": ""
                    }
                },
                {
                    "CurrentOrder": {
                        "OrderNumber": "456",
                        "Status": ""
                    }
                },
                {
                    "CurrentOrder": {
                        "Status": ""
                    }
                }
            ]
        }
    }
}

它给出了正确的结果:

[
  {
    "CurrentOrder": {
      "OrderNumber": "123",
      "Status": ""
    }
  },
  {
    "CurrentOrder": {
      "OrderNumber": "456",
      "Status": ""
    }
  }
]
于 2018-03-16T17:39:30.380 回答