Golang beginners will definitely find Json parsing very troublesome. In fact, we need to change our thinking. We cannot directly convert Json into objects like PHP or JS.
So we define a series of functions to get the values in Json.
gjson (github.com/tidwall/gjson) supports various Json operations very well. It can be used to easily parse Json, judge and obtain values.
2. Result structure
// First define a Result structure, which is the abstraction of all data type Result struct { Type Type // The type of this structure in Json Raw string // original json string Str string // string Num float64 // floating point number Index int // index }
3. Functions belonging to Result
func (t Result) Exists() bool // Determine whether a value exists func (t Result) Value() interface{} // func (t Result) Int() int64 func (t Result) Uint() uint64 func (t Result) Float() float64 func (t Result) String() string func (t Result) Bool() bool func (t Result) Time() time.Time func (t Result) Array() []gjson.Result func (t Result) Map() map[string]gjson.Result func (t Result) Get(path string) Result func (t Result) ForEach(iterator func(key, value Result) bool) // Passable closure function func (t Result) Less(token Result, caseSensitive bool) bool
4. Initialization function
gjson.Parse(json).Get("name").Get("last") gjson.Get(json, "name").Get("last") gjson.Get(json, "name.last")
5. Determine whether Json is legal
if !gjson.Valid(json) { return errors.New("invalid json") }
3. Practical operation
1. Use
package main import ( "fmt" "log" "strings" "github.com/tidwall/gjson" ) const json = `{"name":{"first":"Tom","last":"Anderson"},"age":37,"children":["Sara","Alex","Jack"] ,"fav.movie":"Deer Hunter","friends":[{"first":"Dale","last":"Murphy","age":44},{"first":"Roger", "last":"Craig","age":68},{"first":"Jane","last":"Murphy","age":47}]}` func main() { // First we determine whether the json is legal if !gjson.Valid(json) { log.Fatalf("%s", "invalid json") } // Get the age in Json age := gjson.Get(json, `age`).Int() fmt.Printf("%T, %+v\n", age, age) // Get lastname lastname := gjson.Get(json, `name.last`).String() fmt.Printf("%T, %+v\n", lastname, lastname) // Get the children array for _, v := range gjson.Get(json, `children`).Array() { fmt.Printf("%q ", v.String()) } fmt.Println() // Get the second child fmt.Printf("%q\n", gjson.Get(json, `children.1`).String()) fmt.Printf("%q\n", gjson.Get(json, `children|1`).String()) // Wildcard to get the third child fmt.Printf("%q\n", gjson.Get(json, `child*.2`).String()) //reverse array function fmt.Printf("%q\n", gjson.Get(json, `children|@reverse`).Array()) // Custom function - convert all to uppercase gjson.AddModifier("case", func(json, arg string) string { if arg == "upper" { return strings.ToUpper(json) } return json }) fmt.Printf("%+v\n", gjson.Get(json, `children|@case:upper`).Array()) // Directly parse to map jsonMap := gjson.Parse(json).Map() fmt.Printf("%+v\n", jsonMap) for _, v := range jsonMap { fmt.Printf("%T, %+v\n", v, v) } }
The above is the entire content of this article. I hope it will be helpful to everyone’s study, and I hope you will support us