Last active
December 10, 2019 10:43
-
-
Save wangrenjun/10cd4075c7a85c3d274334536a745a77 to your computer and use it in GitHub Desktop.
GJSON example of ForEach()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "github.com/tidwall/gjson" | |
import "fmt" | |
import "strings" | |
func main() { | |
jsonstring := ` | |
{ | |
"status":"ok", | |
"data":{ | |
"aqi":21, | |
"idx":1451, | |
"attributions":[ | |
{ | |
"url":"http://www.bjmemc.com.cn/", | |
"name":"Beijing Environmental Protection Monitoring Center (北京市环境保护监测中心)" | |
}, | |
{ | |
"url":"https://china.usembassy-china.org.cn/embassy-consulates/beijing/air-quality-monitor/", | |
"name":"U.S Embassy Beijing Air Quality Monitor (美国驻北京大使馆空气质量监测)" | |
}, | |
{ | |
"url":"https://waqi.info/", | |
"name":"World Air Quality Index Project" | |
} | |
], | |
"city":{ | |
"geo":[ | |
39.954592, | |
116.468117 | |
], | |
"name":"Beijing (北京)", | |
"url":"https://aqicn.org/city/beijing" | |
}, | |
"dominentpol":"pm25", | |
"iaqi":{ | |
"co":{ | |
"v":2.8 | |
}, | |
"no2":{ | |
"v":5.1 | |
}, | |
"o3":{ | |
"v":26 | |
}, | |
"pm10":{ | |
"v":15 | |
}, | |
"pm25":{ | |
"v":21 | |
}, | |
"so2":{ | |
"v":0.6 | |
}, | |
"w":{ | |
"v":7.7 | |
} | |
}, | |
"time":{ | |
"s":"2019-12-10 15:00:00", | |
"tz":"+08:00", | |
"v":1575990000 | |
}, | |
"debug":{ | |
"sync":"2019-12-10T16:29:00+09:00" | |
} | |
} | |
} | |
` | |
parsedJson := gjson.Parse(jsonstring) | |
parsedJson.ForEach(jsoniter) | |
} | |
var stack []string | |
func jsoniter(key, v gjson.Result) bool { | |
keypath := fmt.Sprintf("%s", key) | |
if keypath == "" { | |
keypath = "[]" | |
} | |
stack = append(stack, keypath) | |
keypath = strings.Join(stack[:], ".") | |
switch v.Type { | |
case gjson.JSON: | |
v.ForEach(jsoniter) | |
fmt.Printf("%30s %10s\n", keypath, v.Type) | |
case gjson.String: | |
fmt.Printf("%30s %10s %s\n", keypath, v.Type, v.String()) | |
case gjson.Number: | |
fmt.Printf("%30s %10s %s\n", keypath, v.Type, v.String()) | |
case gjson.True: | |
fmt.Printf("%30s %10s %s\n", keypath, v.Type, v.String()) | |
case gjson.False: | |
fmt.Printf("%30s %10s %s\n", keypath, v.Type, v.String()) | |
case gjson.Null: | |
fmt.Printf("%30s %10s %s\n", keypath, v.Type, v.String()) | |
default: | |
panic("error") | |
} | |
stack = stack[:len(stack) - 1] | |
return true | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[rj]$ go run ./gjson_foreach.go | |
status String ok | |
data.aqi Number 21 | |
data.idx Number 1451 | |
data.attributions.[].url String http://www.bjmemc.com.cn/ | |
data.attributions.[].name String Beijing Environmental Protection Monitoring Center (北京市环境保护监测中心) | |
data.attributions.[] JSON | |
data.attributions.[].url String https://china.usembassy-china.org.cn/embassy-consulates/beijing/air-quality-monitor/ | |
data.attributions.[].name String U.S Embassy Beijing Air Quality Monitor (美国驻北京大使馆空气质量监测) | |
data.attributions.[] JSON | |
data.attributions.[].url String https://waqi.info/ | |
data.attributions.[].name String World Air Quality Index Project | |
data.attributions.[] JSON | |
data.attributions JSON | |
data.city.geo.[] Number 39.954592 | |
data.city.geo.[] Number 116.468117 | |
data.city.geo JSON | |
data.city.name String Beijing (北京) | |
data.city.url String https://aqicn.org/city/beijing | |
data.city JSON | |
data.dominentpol String pm25 | |
data.iaqi.co.v Number 2.8 | |
data.iaqi.co JSON | |
data.iaqi.no2.v Number 5.1 | |
data.iaqi.no2 JSON | |
data.iaqi.o3.v Number 26 | |
data.iaqi.o3 JSON | |
data.iaqi.pm10.v Number 15 | |
data.iaqi.pm10 JSON | |
data.iaqi.pm25.v Number 21 | |
data.iaqi.pm25 JSON | |
data.iaqi.so2.v Number 0.6 | |
data.iaqi.so2 JSON | |
data.iaqi.w.v Number 7.7 | |
data.iaqi.w JSON | |
data.iaqi JSON | |
data.time.s String 2019-12-10 15:00:00 | |
data.time.tz String +08:00 | |
data.time.v Number 1575990000 | |
data.time JSON | |
data.debug.sync String 2019-12-10T16:29:00+09:00 | |
data.debug JSON | |
data JSON |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment