JSONPath 是 RedisJSON里非常重要的路径语法,必须要了解掌握的 关键字:Redis,json,RedisJSON,JSONPath [[toc]]
JSONPath
XPath 是提取XML数据的有力工具,而JSONPath用于解决JSON类似的问题,而且有如下的两点思考。
- 客户端不借助特殊的脚本的情况下,数据能被轻松读取
- 客户端请求的 JSON 数据可以缩减部分不于服务器交互,这样可以最大限度地减少服务器响应的带宽使用。
JSONPath 表达式
JSONPath 表达式可以使用点表示法或者是括号记号
$.store.book[0].title
$['store']['book'][0]['title']
JSONPath 允许用通配符符号 *
表示成员名称和数组索引,它借用了 E4X 的后缀运算符 ...
和 ECMASCRIPT 4 的 数组切片语法建议 [start:end:step]
$.store.book[(@.length-1)].title
对当前对象使用符号 @
如下所示,通过语法 ?(<boolean expr>)
支持筛选表达式
$.store.book[?(@.price < 10)].title
这里是一个完整的概述,并且对 JSONPath 语法元素和它的 XPath 对应元素进行了并行比较。
|XPath|JSONPath|说明| |---|---|---| |/|$|根对象| |.|@|当前对象| |/|. or []|子操作符| |..|无|父操作符| |//|..|递归下降| |||通配符| |@|无|读取属性| |[]|[]|下标操作符| |无|[,]|XPath 中的联合运算符节点集的组合。JSONPath 允许将备用名称或数组索引作为一个集合| |无|[start : end : step]|数组切片操作符| |[]|?()|过滤器表达式| |无|()|脚本表达式,使用底层脚本引擎| |()|无|Xpath 的分组|
JSONPath 列子
让我们通过更多的例子来练习 JSONPath 表达式。我们首先从一个表示书店的XML示例之后构建的简单JSON 结构开始。
{ "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
}
}
}
|XPath|JSONPath|说明| |---|---|---| |/store/book/author|$.store.book[].author| 商店里所有书的作者 | |//author|$..author| 所有作者 | |/store/|$.store.| 商店里的所有东西,包括书及自行车 | |/store//price|$.store..price| 商店里的所有东西的价格| |//book[3]|$..book[2]| 第三本书| |//book[last()]|$..book[(@.length-1)] $..book[-1:]| 序列里的最后一本书| |//book[position() < 3]|$..book[0,1] $..book[:2]| 序列里前两本书| |//book[isbn]|$..book[?(@.isbn)]| 根据ISBN来过滤书本| |//book[price<10]|$..book[?(@.price<10)]| 价格小于10的书本| |//|$..*| 所有|
JSONPath 使用
JSONPath 是用 Javascript 实现的,用于客户端的使用,也可以移植到 PHP 以便在服务器上使用。
JS / PHP 库文件
你所需要做的就是下载这两个文件中的一个
将其包含在程序中,并使用由单个函数组成的简单 API。
//obj json结构体
//expr jsonPath的表达式
//args 目前只支持一个参数 args.resultType ("VALUE"|"PATH")
jsonPath(obj, expr [, args])
JavaScript 例子
var o = { /*...*/ }, // the 'store' JSON object from above
res1 = jsonPath(o, "$..author").toJSONString(),
res2 = jsonPath(o, "$..author", {resultType:"PATH"}).toJSONString();
PHP 例子
require_once('json.php'); // JSON parser
require_once('jsonpath.php'); // JSONPath evaluator
$json = '{ ... }'; // JSON structure from above
$parser = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
$o = $parser->decode($json);
$match1 = jsonPath($o, "$..author");
$match2 = jsonPath($o, "$..author", array("resultType" => "PATH"));
$res1 = $parser->encode($match1);
$res2 = $parser->encode($match2);
运行结果
不管是JS还是PHP结果返回都是JSON数组
res1:
[ "Nigel Rees",
"Evelyn Waugh",
"Herman Melville",
"J. R. R. Tolkien"
]
res2:
[ "$['store']['book'][0]['author']",
"$['store']['book'][1]['author']",
"$['store']['book'][2]['author']",
"$['store']['book'][3]['author']"
]