简体   繁体   English

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

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

kI have a problem with the library https://github.com/JSONPath-Plus/JSONPath at the latest version. k我对最新版本的库https://github.com/JSONPath-Plus/JSONPath有疑问。

Example:例子:

{
    "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"
        }
    ]
}

Now i want to get "phoneNumbers [1]" with现在我想得到“phoneNumbers [1]”

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

Here is a Demo Page for testing: https://jsonpath-plus.github.io/JSONPath/demo/这是一个用于测试的演示页面: https ://jsonpath-plus.github.io/JSONPath/demo/

That did not work because of the square brackets inside of the String.由于字符串内部的方括号,这不起作用。 Do anyone know how to solve this Problem.有谁知道如何解决这个问题。

I have a lot of Json Objects containing strings with square brackets, so if there is a solution not only for a specific case.我有很多 Json 对象,其中包含带方括号的字符串,所以如果有解决方案不仅适用于特定情况。

Is there any option to escape characters so that the problem will be solved?是否有任何转义字符的选项以便解决问题? The json Object above is only a Example Code to describe the Problem.上面的 json 对象只是描述问题的示例代码。

The Problem also occurs when only using JSONPath so it is not limited to that Library.仅使用 JSONPath 时也会出现此问题,因此它不限于该库。

https://jsonpath.com/ https://jsonpath.com/

This appears to be a bug in JSONPath-Plus.这似乎是 JSONPath-Plus 中的一个错误。 The jsonpath library can handle it. 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>

There's not much you can do about this beyond:除此之外,您无能为力:

  • Changing your data structure改变你的数据结构
  • Changing your library改变你的图书馆
  • Filing a bug report提交错误报告

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

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

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

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