基本上,我需要使用 JSONPath 获取字符串值的一部分。我无法先使用 JSONPath 获取完整值,然后再使用另一种语言获取其中的一部分。
有没有办法在 JSONPath 中做到这一点?
如果您可以举例说明一些 JSON 数据,那就太好了。根据你所写的,我怀疑你正在寻找这样的东西(这里是 JSFilddle:http: //jsfiddle.net/hbi99/4WYkc/);
var data = {
"store": {
"book": [
{
"@price": 12.99,
"title": "Sword of Honour",
"category": "fiction",
"author": "Evelyn Waugh"
},
{
"@price": 22.99,
"title": "The Lord of the Rings",
"category": "fiction",
"author": "J. R. R. Tolkien",
"isbn": "0-395-19395-8"
}
],
"bicycle": {
"@price": 19.95,
"brand": "Cannondale",
"color": "red"
}
}
},
found = JSON.search(data, '//*[contains(title, "Lord")]');
document.getElementById('output').innerHTML = found[0].title;
JSONPath 不是标准化的“查询语言”,但 XPath 是。JS 库 DefiantJS 使用“search”方法扩展了全局对象 JSON,您可以使用该方法使用 XPath 表达式查询 JSON 结构。该方法将匹配项作为类似数组的对象返回。
要查看更多示例,请在此处查看实时 XPath Evaluator:http: //defiantjs.com/#xpath_evaluator