键或值以@字符开头的JSONPath?

JSONPath where key or value starts with @ character?

我正在尝试使用 JSONPath 获取基于包含 @ 符号的键的值失败。
原因:由于@是当前的object/element,json不是return的值。

示例 1:

{
  "firstName": "John",
  "lastName" : "doe",
  "phoneNumbers": [
    {
      "type"  : "iPhone",
      "number": "0123-4567-8888"
    },
    {
      "type"  : "home",
      "number": "0123-4567-8910"
    }
  ]
}

这个有效:$.phoneNumbers[1].type
这有效:$.phoneNumbers[?(@.type=="iPhone")].type

示例 2:

{
  "firstName": "John",
  "lastName" : "doe",
  "phoneNumbers": [
    {
      "@type"  : "iPhone",
      "number": "0123-4567-8888"
    },
    {
      "@type"  : "home",
      "number": "0123-4567-8910"
    }
  ]
}

这个有效:$.phoneNumbers[1].type
这不起作用: $.phoneNumbers[?(@.@type=="iPhone")].type

对于处理包含 @ 个字符的键和值有什么建议吗?

这取决于您使用的是哪个 jsonpath 实现。
如果您使用的是 JsonPath-Plus 那么您可以尝试这样的操作:

$.phoneNumbers[?(@['@type'] == "iPhone")]

你可以在线评估jsonpath here 是基于JsonPath-Plus.