gojson是快速解析json数据的一个golang包,你使用它可以快速的查找json内的数据
安装
go get github.com/widuu/gojson
使用简介
结构
type Js struct { data interface{} }
json := `{"from":"en","to":"zh"}` c1 := gojson.Json(json) //&{map[from:en to:zh]}
json := `{"from":"en","to":"zh","trans_result":{"src":"today","dst":"\u4eca\u5929"},"result":["src","today","dst","\u4eca\u5929"]}`c2 := gojson.Json(json).Get("trans_result").Get("dst") fmt.Println(c2) //&{今天}
c2 := gojson.Json(json).Get("from") fmt.Println(c2) //&{en}
c2 := gojson.Json(json).Get("from").Tostring() fmt.Println(c2) //en
c4 := gojson.Json(json).Getpath("trans_result", "src").Tostring() fmt.Println(c4) //today
json := `{"from":"en","to":"zh","trans_result":{"src":"today","dst":"\u4eca\u5929"},"result":["src","today","dst","\u4eca\u5929"]}` c7 := gojson.Json(json).Get("result").Arrayindex(1) fmt.Println(c7) //src
json1 := `{"from":"en","to":"zh","trans_result":[{"src":"today","dst":"\u4eca\u5929"},{"src":"tomorrow","dst":"\u660e\u5929"}]}` c8 := gojson.Json(json1).Get("trans_result").Getkey("src", 1).Tostring() fmt.Println(c8) //则返回trans_result第一组中的src today
c9k, c9v := gojson.Json(json1).Get("trans_result").ToArray() fmt.Println(c9k, c9v) //[src dst src dst] [today 今天 tomorrow 明天]c3k, c3v := gojson.Json(json).Getindex(1).ToArray() fmt.Println(c3k, c3v) // [from] [en]
json1 := `{"from":"en","to":"zh","trans_result":[{"src":"today","dst":"\u4eca\u5929"},{"src":"tomorrow","dst":"\u660e\u5929"}]}`c10 := gojson.Json(json1).Getindex(3).Getindex(1).Getindex(1).Get("src").Tostring() fmt.Println(c10) //today
c11 := gojson.Json(json).Get("result").StringtoArray() fmt.Println(c11) //[src today dst 今天]
gojson.Json(json).Get("result").Type() //[]interface {}