jsonpath golang 的使用范例
2021-01-14 本文已影响0人
Nick_4438
快速入门
- 安装依赖
go get github.com/oliveagle/jsonpath
- 例子程序
package main
import (
"encoding/json"
"fmt"
// 0. 安装该依赖
// go get github.com/oliveagle/jsonpath
"github.com/oliveagle/jsonpath"
)
var dataStr string = `
{
"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
}
`
func main(){
test1()
test2()
test3()
}
func test1() {
// 1. 数据准备把字符串转换到对象内存储
var json_data interface{}
json.Unmarshal([]byte(dataStr), &json_data)
res, err := jsonpath.JsonPathLookup(json_data, "$.expensive")
if err == nil {
fmt.Println("step 1 res: $.expensive")
fmt.Println(res)
}
}
func test2() {
var json_data interface{}
json.Unmarshal([]byte(dataStr), &json_data)
//or reuse lookup pattern
pat, _ := jsonpath.Compile(`$.store.book[?(@.price < $.expensive)].price`)
res, _ := pat.Lookup(json_data)
fmt.Println("step 2 res:")
fmt.Println(res)
}
func test3() {
// 3. 未找到对象
var json_data interface{}
json.Unmarshal([]byte(dataStr), &json_data)
res, err := jsonpath.JsonPathLookup(json_data, "$.expensive1")
if err == nil {
fmt.Println("step 3 res: $.expensive")
fmt.Println(res)
} else {
fmt.Printf("dddd: %v ",err)
}
}
符号定义
和java项目github.com/jayway/JsonPath
的符号类似,一些差异如下:
Operator | Supported | Description |
---|---|---|
$ | Y | json的根节点. 通常在表达式开始位置. |
@ | Y | The current node being processed by a filter predicate. |
* | X | Wildcard. Available anywhere a name or numeric are required. |
.. | X | Deep scan. Available anywhere a name is required. |
. | Y | Dot-notated child |
['' (, '')] | X | Bracket-notated child or children |
[ (, )] | Y | Array index or indexes |
[start:end] | Y | Array slice operator |
[?()] | Y | Filter expression. Expression must evaluate to a boolean value. |
例子数据
{
"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 | result |
---|---|
$.expensive | 10 |
$.store.book[0].price | 8.95 |
$.store.book[-1].isbn | "0-395-19395-8" |
$.store.book[0,1].price | [8.95, 12.99] |
$.store.book[0:2].price | [8.95, 12.99, 8.99] |
$.store.book[?(@.isbn)].price | [8.99, 22.99] |
$.store.book[?(@.price > 10)].title | ["Sword of Honour", "The Lord of the Rings"] |
.expensive)].price | [8.95, 8.99] |
$.store.book[:].price | [8.9.5, 12.99, 8.9.9, 22.99] |
$.store.book[?(@.author =~ /(?i).*REES/)].author | "Nigel Rees" |
Note: golang 支持正则表达式标志,格式如 (?imsU)pattern