- 使用 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>