- JSONPath - 是xpath在json的應用。
- JSONPath 表達式
$.store.book[0].title
$['store']['book'][0]['title']
[start:end:step]是從ECMASCRIPT
4 參照過來的。
$.store.book[(@.length-1)].title
$.store.book[?(@.price < 10)].title
XPath | JSONPath | Description |
/ | $ | 表示根元素 |
. | @ | 當前元素 |
/ | . or [] | 子元素 |
.. | n/a | 父元素 |
// | .. | 遞歸下降,JSONPath是從E4X借鑑的。 |
* | * | 通配符,表示所有的元素 |
@ | n/a | 屬性訪問字符 |
[] | [] |
子元素操作符
|
| | [,] |
連接操作符在XPath 結果合併其它結點集合。JSONP允許name或者數組索引。
|
n/a | [start:end:step] |
數組分割操作從ES4借鑑。
|
[] | ?() |
應用過濾表示式
|
n/a | () |
腳本表達式,使用在腳本引擎下面。
|
() | n/a | Xpath分組 |
- []在xpath表達式總是從前面的路徑來操作數組,索引是從1開始。
- 使用JOSNPath的[]操作符操作一個對象或者數組,索引是從0開始。
- SONPath 例子
{ "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的所有元素。所有的bookst和bicycle
|
/store//price |
$.store..price |
store裏面所有東西的price
|
//book[3] |
$..book[2] |
第三個書
|
//book[last()] |
$..book[(@.length-1)] |
最後一本書 |
//book[position()<3] |
$..book[0,1]
$..book[:2] |
前面的兩本書。 |
//book[isbn] |
$..book[?(@.isbn)] |
過濾出所有的包含isbn的書。 |
//book[price<10] |
$..book[?(@.price<10)] |
過濾出價格低於10的書。 |
//* |
$..* |
所有元素。
|