golang 超好用 json 解析包 介绍 使用

当遇到接收的json的结构不明确的时候,每次想获取json里面的字段,都要不断断言字段的类型,使用起来特别不方便。

这里推荐一个第三方包
https://github.com/tidwall/gjson

使用

go get -u github.com/tidwall/gjson
  • 1
package main

import "github.com/tidwall/gjson"

const json = `{"name":{"first":"Janet","last":"Prichard"},"age":47}`

func main() {
	value := gjson.Get(json, "name.last")
	println(value.String())
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

详细的使用方式这里不做介绍,请移步gihub, 记得随手帮作者点个star~