如何解决JSONPath,其中键或值以@字符开头?
我正在尝试使用JSONPath基于包含@符号的键获取值失败。
原因:由于@是当前对象/元素,因此json不返回该值。
示例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","phoneNumbers": [
{
"@type" : "iPhone",{
"@type" : "home","number": "0123-4567-8910"
}
]
}
这有效:$.phoneNumbers[1].type
这不起作用: $.phoneNumbers[?(@.@type=="iPhone")].type
关于处理包含@
个字符的键和值的任何建议吗?
解决方法
这取决于您所使用的jsonpath实现。
如果您使用的是JsonPath-Plus,则可以尝试以下操作:
$.phoneNumbers[?(@['@type'] == "iPhone")]
您可以在线here评估基于JsonPath-Plus
的jsonpath。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。