JSONPath(XPath for JSON)解析 JSON教程

1. 介紹

類似於XPath在xml文檔中的定位,JsonPath表達式通常是用來路徑檢索或設置Json的。其表達式可以接受“dot–notation”和“bracket–notation”格式,例如$.store.book[0].title、$[‘store’][‘book’][0][‘title’]

JsonPath是一種簡單的方法來提取給定JSON文檔的部分內容。 JsonPath有許多編程語言,如Javascript,Python和PHP,Java。JsonPath提供的json解析非常強大,它提供了類似正則表達式的語法,基本上可以滿足所有你想要獲得的json內容。

github上有它的應用:https://github.com/json-path/JsonPath

2. 操作符

符號 描述
$ 查詢的根節點對象,用於表示一個json數據,可以是數組或對象
@ 過濾器斷言(filter predicate)處理的當前節點對象,類似於java中的this字段
* 通配符,可以表示一個名字或數字
.. 可以理解爲遞歸搜索,Deep scan. Available anywhere a name is required.
.<name> 表示一個子節點
[‘<name>’ (, ‘<name>’)] 表示一個或多個子節點
[<number> (, <number>)] 表示一個或多個數組下標
[start:end] 數組片段,區間爲[start,end),不包含end
[?(<expression>)] 過濾器表達式,表達式結果必須是boolean

3. 函數

可以在JsonPath表達式執行後進行調用,其輸入值爲表達式的結果。

名稱 描述 輸出
min() 獲取數值類型數組的最小值 Double
max() 獲取數值類型數組的最大值 Double
avg() 獲取數值類型數組的平均值 Double
stddev() 獲取數值類型數組的標準差 Double
length() 獲取數值類型數組的長度 Integer

4. 過濾器

過濾器是用於過濾數組的邏輯表達式,一個通常的表達式形如:[?(@.age > 18)],可以通過邏輯表達式&&或||組合多個過濾器表達式,例如[?(@.price < 10 && @.category == ‘fiction’)],字符串必須用單引號或雙引號包圍,例如[?(@.color == ‘blue’)] or [?(@.color == “blue”)]。

操作符 描述
== 等於符號,但數字1不等於字符1(note that 1 is not equal to ‘1’)
!= 不等於符號
< 小於符號
<= 小於等於符號
> 大於符號
>= 大於等於符號
=~ 判斷是否符合正則表達式,例如[?(@.name =~ /foo.*?/i)]
in 所屬符號,例如[?(@.size in [‘S’, ‘M’])]
nin 排除符號
size size of left (array or string) should match right
empty 判空符號

5. 示例

{
    "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表達式 結果
$.store.book[*].author 
或 
$..author
[
“Nigel Rees”,
“Evelyn Waugh”,
“Herman Melville”,
“J. R. R. Tolkien”
]
$.store.* 顯示所有葉子節點值 [
[
{
”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)] 所有價格小於10的書
$..book[?(@.price <= $[‘expensive’])] 所有價格低於expensive字段的書
$..book[?(@.author =~ /.*REES/i)] 所有符合正則表達式的書 
[
{
”category” : “reference”,
”author” : “Nigel Rees”,
”title” : “Sayings of the Century”,
”price” : 8.95
}
]
$..* 返回所有
$..book.length() [
4
]

JSONPath在線解析器:http://www.atoolbox.net/Tool.php?Id=792

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章