我有超過20000個對象的巨大JSON文件。我想在這個json文件中查詢一個字符串。但是,使用jsonpath
,我只能找到完全匹配?使用jsonpath進行字符串搜索
比方說,我的文件是這樣的:
{ "store": {
"book": [
{ "category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{ "category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{ "category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{ "category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
}
}
我想要做的是:
jsonPath(data, "$..book[?(@.title like 'ord')]")
,並在其標題爲 「ORD」 買書。
{ "category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
{ "category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
}
是否有可能?
感謝您的回覆。但是我在jsonpath中找不到「like」運算符?你的意思是不支持? – huzeyfe
我錯了。顯然LIKE運算符不被支持。但是,我確實在jsonPath單元測試中找到了正則表達式的用法。我已更新了該鏈接的答案。 – Homer6
請注意,這是使用eval來處理此解決方案。因此,如果您使用用戶提交的數據進行搜索(可能是這種情況),您將不得不擔心正確過濾和轉義輸入,以致惡意用戶無法代表其他用戶執行代碼(或者如果這個jsonPath通過node.js在服務器端運行)。 – Homer6