基于golang的Json选择器

20 篇文章 0 订阅
15 篇文章 0 订阅
  • 使用 https://github.com/mangenotwork/gathertool 库的JsonFind()函数来选择json

func JsonFind(jsonStr, find string) (interface{}, error) {}

参数说明
// JsonFind 按路径寻找指定json值
// @find : 寻找路径,与目录的url类似, 下面是一个例子:
// json:  {a:[{b:1},{b:2}]}
// find=/a/[0]  =>   {b:1}
// find=a/[0]/b  =>   1

json 例子:

{
    "reason":"查询成功!",
    "result":{
        "city":"苏州",
        "realtime":{
            "temperature":"17",
            "humidity":"69",
            "info":"阴",
            "wid":"02",
            "direct":"东风",
            "power":"2级",
            "aqi":"30"
        },
        "future":[
            {
                "date":"2021-10-25",
                "temperature":"12\/21℃",
                "weather":"多云",
                "wid":{
                    "day":"01",
                    "night":"01"
                },
                "direct":"东风"
            },
            {
                "date":"2021-10-26",
                "temperature":"13\/21℃",
                "weather":"多云",
                "wid":{
                    "day":"01",
                    "night":"01"
                },
                "direct":"东风转东北风"
            },
            {
                "date":"2021-10-27",
                "temperature":"13\/22℃",
                "weather":"多云",
                "wid":{
                    "day":"01",
                    "night":"01"
                },
                "direct":"东北风"
            },
            {
                "date":"2021-10-28",
                "temperature":"13\/21℃",
                "weather":"多云转晴",
                "wid":{
                    "day":"01",
                    "night":"00"
                },
                "direct":"东北风"
            },
            {
                "date":"2021-10-29",
                "temperature":"14\/21℃",
                "weather":"多云转小雨",
                "wid":{
                    "day":"01",
                    "night":"07"
                },
                "direct":"东北风"
            }
        ]
    },
    "error_code":0
}

如上面的json,要选择出future的第一个的date的值,

import (
	gt "github.com/mangenotwork/gathertool"
	"log"
)

func main(){
    // txt 是上文的json
   log.Println( gt.JsonFind(txt, "/result/future/[0]/date"))
}

输出: 2022/07/29 10:23:08 2021-10-25 < nil >

更多例子

package main

import (
	gt "github.com/mangenotwork/gathertool"
	"log"
)

func main(){
	txt := `{
    "reason":"查询成功!",
    "result":{
        "city":"苏州",
        "realtime":{
            "temperature":"17",
            "humidity":"69",
            "info":"阴",
            "wid":"02",
            "direct":"东风",
            "power":"2级",
            "aqi":"30"
        },
        "future":[
            {
                "date":"2021-10-25",
                "temperature":"12\/21℃",
                "weather":"多云",
                "wid":{
                    "day":"01",
                    "night":"01"
                },
                "direct":"东风"
            },
            {
                "date":"2021-10-26",
                "temperature":"13\/21℃",
                "weather":"多云",
                "wid":{
                    "day":"01",
                    "night":"01"
                },
                "direct":"东风转东北风"
            },
            {
                "date":"2021-10-27",
                "temperature":"13\/22℃",
                "weather":"多云",
                "wid":{
                    "day":"01",
                    "night":"01"
                },
                "direct":"东北风"
            },
            {
                "date":"2021-10-28",
                "temperature":"13\/21℃",
                "weather":"多云转晴",
                "wid":{
                    "day":"01",
                    "night":"00"
                },
                "direct":"东北风"
            },
            {
                "date":"2021-10-29",
                "temperature":"14\/21℃",
                "weather":"多云转小雨",
                "wid":{
                    "day":"01",
                    "night":"07"
                },
                "direct":"东北风"
            }
        ]
    },
    "error_code":0
}`

	jx1 := "/result/future/[0]/date"
	jx2 := "/result/future/[0]"
	jx3 := "/result/future"

	log.Println(gt.JsonFind(txt, jx1))
	log.Println(gt.JsonFind2Json(txt, jx2))
	log.Println(gt.JsonFind2Json(txt, jx3))
	log.Println(gt.JsonFind2Map(txt, jx2))
	log.Println(gt.JsonFind2Arr(txt, jx3))

}

输出

2022/07/29 10:23:08 2021-10-25 <nil>
2022/07/29 10:23:08 {"date":"2021-10-25","direct":"东风","temperature":"12/21℃","weather":"多云","wid":{"day":"01","night":"01"}} <nil>
2022/07/29 10:23:08 [{"date":"2021-10-25","direct":"东风","temperature":"12/21℃","weather":"多云","wid":{"day":"01","night":"01"}},{"date":"2021-10-26","direct":"东风转东北风","temperature":"13/21℃","weather":"多云","wid":{"day":"01","night":"01"}},{"date":"2021-10-27","direct":"东北风","temperature":"13/22℃","weather":"多云","wid":{"day":"01","night":"01"}},{"date":"2021-10-28","direct":"东北风","temperature":"13/21℃","weather":"多云转晴","wid":{"day":"01","night":"00"}},{"date":"2021-10-29","direct":"东北风","temperature":"14/21℃","weather":"多云转小雨","wid":{"day":"01","night":"07"}}] <nil>
2022/07/29 10:23:08 map[date:2021-10-25 direct:东风 temperature:12/21℃ weather:多云 wid:map[day:01 night:01]] <nil>
2022/07/29 10:23:08 [map[date:2021-10-25 direct:东风 temperature:12/21℃ weather:多云 wid:map[day:01 night:01]] map[date:2021-10-26 direct:东风转东北风 temperature:13/21℃ weather:多云 wid:map[day:01 night:01]] map[date:2021-10-27 direct:东北风 temperature:13/22℃ weather:多云 wid:map[day:01 night:01]] map[date:2021-10-28 direct:东北风 temperature:13/21℃ weather:多云转晴 wid:map[day:01 night:00]] map[date:2021-10-29 direct:东北风 temperature:14/21℃ weather:多云转小雨 wid:map[day:01 night:07]]] <nil>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值