[英]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 時也會出現此問題,因此它不限於該庫。
這似乎是 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.