需求: 将一个字符串。它由多个json组成并以,分隔。想提取出各个json中指定的key,因为想通过gitlab api获取项目分支,而获取到的字符串就是这样的格式
测试的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
}
},
"expensive": 10
}
主要代码
package api
import (
"encoding/json"
"fmt"
"github.com/yalp/jsonpath"
)
type BranchController struct {
BaseController
}
func (c *BranchController) GetBranch() {
//1.准备一段json
raw := []byte(`[
{
"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
}
]`)
helloFilter, err := jsonpath.Prepare("$..price")
if err != nil {
panic(err)
}
var data interface{}
if err = json.Unmarshal(raw, &data); err != nil {
panic(err)
}
out, err := helloFilter(data)
if err != nil {
panic(err)
}
fmt.Print(out)
c.Ctx.WriteString("获取配置成功")
}
JsonPath (click link to try) | Result |
---|---|
[KaTeX parse error: Can't use function '\]' in math mode at position 14: .store.book[*\̲]̲.author](http:/….store.book[*].author) | The authors of all books |
[ . . a u t h o r ] ( h t t p : / / j s o n p a t h . h e r o k u a p p . c o m / ? p a t h = ..author](http://jsonpath.herokuapp.com/?path= ..author](http://jsonpath.herokuapp.com/?path=…author) | All authors |
[ . s t o r e . ∗ ] ( h t t p : / / j s o n p a t h . h e r o k u a p p . c o m / ? p a t h = .store.*](http://jsonpath.herokuapp.com/?path= .store.∗](http://jsonpath.herokuapp.com/?path=.store.*) | All things, both books and bicycles |
[ . s t o r e . . p r i c e ] ( h t t p : / / j s o n p a t h . h e r o k u a p p . c o m / ? p a t h = .store..price](http://jsonpath.herokuapp.com/?path= .store..price](http://jsonpath.herokuapp.com/?path=.store…price) | The price of everything |
[KaTeX parse error: Can't use function '\]' in math mode at position 9: ..book[2\̲]̲](http://jsonpa……book[2]) | The third book |
[KaTeX parse error: Can't use function '\]' in math mode at position 10: ..book[-2\̲]̲](http://jsonpa……book[2]) | The second to last book |
[KaTeX parse error: Can't use function '\]' in math mode at position 11: ..book[0,1\̲]̲](http://jsonpa……book[0,1]) | The first two books |
[KaTeX parse error: Can't use function '\]' in math mode at position 10: ..book[:2\̲]̲](http://jsonpa……book[:2]) | All books from index 0 (inclusive) until index 2 (exclusive) |
[KaTeX parse error: Can't use function '\]' in math mode at position 11: ..book[1:2\̲]̲](http://jsonpa……book[1:2]) | All books from index 1 (inclusive) until index 2 (exclusive) |
[KaTeX parse error: Can't use function '\]' in math mode at position 11: ..book[-2:\̲]̲](http://jsonpa……book[-2:]) | Last two books |
[KaTeX parse error: Can't use function '\]' in math mode at position 10: ..book[2:\̲]̲](http://jsonpa……book[2:]) | Book number two from tail |
[KaTeX parse error: Can't use function '\]' in math mode at position 17: ….book[?(@.isbn)\̲]̲](http://jsonpa……book[?(@.isbn)]) | All books with an ISBN number |
[KaTeX parse error: Can't use function '\]' in math mode at position 28: …?(@.price < 10)\̲]̲](http://jsonpa….store.book[?(@.price < 10)]) | All books in store cheaper than 10 |
[$…book[?(@.price <= KaTeX parse error: Can't use function '\]' in math mode at position 13: ['expensive'\̲]̲)]](http://json……book[?(@.price <= $[‘expensive’])]) | All books in store that are not “expensive” |
[KaTeX parse error: Can't use function '\]' in math mode at position 32: …r =~ /.*REES/i)\̲]̲](http://jsonpa……book[?(@.author =~ /.*REES/i)]) | All books matching regex (ignore case) |
[ . . ∗ ] ( h t t p : / / j s o n p a t h . h e r o k u a p p . c o m / ? p a t h = ..*](http://jsonpath.herokuapp.com/?path= ..∗](http://jsonpath.herokuapp.com/?path=…*) | Give me every thing |
[ . . b o o k . l e n g t h ( ) ] ( h t t p : / / j s o n p a t h . h e r o k u a p p . c o m / ? p a t h = ..book.length()](http://jsonpath.herokuapp.com/?path= ..book.length()](http://jsonpath.herokuapp.com/?path=…book.length()) | The number of books |
输出结果
如果是gitlab 分支
也可以在线测试
地址https://play.golang.org/
参考
https://github.com/json-path/JsonPath
https://bbs.csdn.net/topics/397685692