摘要:與xml文檔中XPath的定位類似,JsonPath表達(dá)式通常用于檢索或設(shè)置Json路徑。使用。符號(hào):$。百貨商 ... 展開(kāi) 一、對(duì)于json數(shù)據(jù)的解析 簡(jiǎn)介 - JSONPath - 是xpath在json的應(yīng)用。
類似于XPath在xml文檔中的定位,JsonPath表達(dá)式通常是用來(lái)路徑檢索或設(shè)置Json的。其表達(dá)式可以接受“dot–notation”和“bracket–notation”格式,例如$.store.book[0].title、$['store’]['book’][0]['title’] - JSONPaht 用一個(gè)抽象的名字$來(lái)表示最外層對(duì)象。
- 使用.符號(hào):$.store.book[0].title
- 使用[]:$['store']['book'][0]['title']
- 數(shù)組索引
1)JSONPath 允許使用通配符 * 表示所以的子元素名和數(shù)組索引。還允許使用 '..' 從E4X參照過(guò)來(lái)的和數(shù)組切分語(yǔ)法[start:end:step] 2)$.store.book[(@.length-1)].title
3)使用'@'符號(hào)表示當(dāng)前的對(duì)象,?(<判斷表達(dá)式>) 使用邏輯表達(dá)式來(lái)過(guò)濾 $.store.book[?(@.price < 10)].title 二、JSONPath語(yǔ)法元素和對(duì)應(yīng)XPath元素的對(duì)比 XPath | JSONPath | Description | / | $ | 表示根元素 | . | @ | 當(dāng)前元素 | / | . or [] | 子元素 | .. | n/a | 父元素 | // | .. | 遞歸下降,JSONPath是從E4X借鑒的。 | * | * | 通配符,表示所有的元素 | @ | n/a | 屬性訪問(wèn)字符 | [] | [] | 子元素操作符 | | | [,] | 連接操作符在XPath 結(jié)果合并其它結(jié)點(diǎn)集合。JSONP允許name或者數(shù)組索引。 | n/a | [start:end:step] | 數(shù)組分割操作從ES4借鑒。 | [] | ?() | 應(yīng)用過(guò)濾表示式 | n/a | () | 腳本表達(dá)式,使用在腳本引擎下面。 | () | n/a | Xpath分組 | 三、jsonpath使用舉例 接口返回: - [{
- "id": "PRIMARY",
- "name": "小學(xué)",
- "front_id": "PRIMARY",
- "front_name": "小學(xué)"
- }, {
- "id": "JUNIOR",
- "name": "初中",
- "front_id": "JUNIOR",
- "front_name": "初中"
- }, {
- "id": "HIGH",
- "name": "高中",
- "front_id": "HIGH",
- "front_name": "高中"
- }, {
- "id": "TECHNICAL",
- "name": "中專/技校",
- "front_id": "TECHNICAL",
- "front_name": "中專/技校"
- }, {
- "id": "COLLEGE",
- "name": "大專",
- "front_id": "COLLEGE",
- "front_name": "大專"
- }, {
- "id": "BACHELOR",
- "name": "本科",
- "front_id": "BACHELOR",
- "front_name": "本科"
- }, {
- "id": "MASTER",
- "name": "碩士",
- "front_id": "MASTER",
- "front_name": "碩士"
- }, {
- "id": "DOCTOR",
- "name": "博士",
- "front_id": "DOCTOR",
- "front_name": "博士"
- }]
| JSONPath | 結(jié)果 |
| $.[*].name
| 所有學(xué)歷的name |
| $.[*].id
| 所有的id |
| $.[*]
| 所有元素 |
| $.[(@.length-2)].name
| 倒數(shù)第二個(gè)元素的name |
| $.[2]
| 第三個(gè)元素 |
| $.[(@.length-1)]
| 最后一個(gè)元素 |
| $.[0,1]
$.[:2]
| 前面的兩個(gè)元素 |
| $.[?(@.name =~ /.*中/i)]
| 過(guò)濾出所有的name包含“中”的書。 |
| $..book[?(@.price<10)]
| 過(guò)濾出價(jià)格低于10的書。 |
| $.[*].length()
| 所有元素的個(gè)數(shù) |
接口返回: - {
- "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
- }
- },
- "expensive": 10
- }
JsonPath表達(dá)式 | 結(jié)果 | $.store.book[*].author 或 $..author | [ “Nigel Rees”, “Evelyn Waugh”, “Herman Melville”, “J. R. R. Tolkien” ] | $.store.* 顯示所有葉子節(jié)點(diǎn)值 | [ [ { ”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 } ], { ”color” : “red”, ”price” : 19.95 } ] | $.store..price | [ 8.95, 12.99, 8.99, 22.99, 19.95 ] | $..book[0,1] 或 $..book[:2] | [ { ”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 } ] | $..book[-2:] | 獲取最后兩本書 | $..book[2:] | [ { ”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 } ] | $..book[?(@.isbn)] | 所有具有isbn屬性的書 | $.store.book[?(@.price < 10)] | 所有價(jià)格小于10的書 | $..book[?(@.price <= $['expensive’])] | 所有價(jià)格低于expensive字段的書 | $..book[?(@.author =~ /.*REES/i)] | 所有符合正則表達(dá)式的書 [ { ”category” : “reference”, ”author” : “Nigel Rees”, ”title” : “Sayings of the Century”, ”price” : 8.95 } ] | $..* | 返回所有 | $..book.length() | [ 4 ] | 四、過(guò)濾器 操作符 | 描述 | == | 等于符號(hào),但數(shù)字1不等于字符1(note that 1 is not equal to '1’) | != | 不等于符號(hào) | < | 小于符號(hào) | <= | 小于等于符號(hào) | > | 大于符號(hào) | >= | 大于等于符號(hào) | =~ | 判斷是否符合正則表達(dá)式,例如[?(@.name =~ /foo.*?/i)] | in | 所屬符號(hào),例如[?(@.size in ['S’, 'M’])] | nin | 排除符號(hào) | size | size of left (array or string) should match right | empty | 判空符號(hào) |
例如: 1)所有具有isbn屬性的書 $.store.book[?(@.isbn)].author
2)所有價(jià)格大于10的書 $.store.book[?(@.price > 10)]
3)查詢xxx==3的所有對(duì)象 $.result.list[?(@.xxx ==3)] 4)可以自定義過(guò)濾器來(lái)獲取想要的任何元素,可以多條件查詢 五、在線解析器 http:/// https://jsonpath./ 在這里,你可以將你的json格式的數(shù)據(jù)拷貝上去,自己手動(dòng)寫表達(dá)式解析查看。
|