[golang]-golang获取多个json字符串中的指定key的值

需求: 将一个字符串。它由多个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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爷来辣

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值