繁体   English   中英

字符串中的方括号时JSONPath出错

[英]JSONPath getting Error when square brackets inside of string

k我对最新版本的库https://github.com/JSONPath-Plus/JSONPath有疑问。

例子:

{
    "firstName": "John",
    "lastName": "doe",
    "age": 26,
    "address": {
        "streetAddress": "naist street",
        "city": "Nara",
        "postalCode": "630-0192"
    },
    "phoneNumbers [1]": [
        {
            "type": "iPhone",
            "number": "0123-4567-8888"
        },
        {
            "type": "home",
            "number": "0123-4567-8910"
        }
    ]
}

现在我想得到“phoneNumbers [1]”

path: $..['phoneNumbers [1]']

这是一个用于测试的演示页面: https ://jsonpath-plus.github.io/JSONPath/demo/

由于字符串内部的方括号,这不起作用。 有谁知道如何解决这个问题。

我有很多 Json 对象,其中包含带方括号的字符串,所以如果有解决方案不仅适用于特定情况。

是否有任何转义字符的选项以便解决问题? 上面的 json 对象只是描述问题的示例代码。

仅使用 JSONPath 时也会出现此问题,因此它不限于该库。

https://jsonpath.com/

这似乎是 JSONPath-Plus 中的一个错误。 jsonpath 库可以处理它。

 const data = JSON.parse(`{ "firstName": "John", "lastName": "doe", "age": 26, "address": { "streetAddress": "naist street", "city": "Nara", "postalCode": "630-0192" }, "phoneNumbers [1]": [ { "type": "iPhone", "number": "0123-4567-8888" }, { "type": "home", "number": "0123-4567-8910" } ] }`); const result = jsonpath.query(data, "$..['phoneNumbers [1]']"); console.log(result);
 <script src="https://unpkg.com/jsonpath@1.1.1/jsonpath.js"></script>

除此之外,您无能为力:

  • 改变你的数据结构
  • 改变你的图书馆
  • 提交错误报告

有一个只能与jsonpath-plus一起使用的解决方法

$[?(@property == "phoneNumbers [1]")]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM