gojson is used in Go language programs to parse JSON files

  • 2020-05-30 20:22:47
  • OfStack

gojson is an golang package that quickly parses json data. You can use it to quickly look up json data
The installation

 go get github.com/widuu/gojson

Use profile


type Js struct {
    data interface{}

(1) func Json(data) *Js data is of type string, initializes the Js structure, parses json and return Js.data

json := `{"from":"en","to":"zh"}`
c1 := gojson.Json(json) //&{map[from:en to:zh]}

(2) func (*Js) Get() *js gets a value in the simple json, finds it recursively, return Js.data

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) //&{ Today, } c2 := gojson.Json(json).Get("from")
fmt.Println(c2) //&{en}

(3) func (*Js)Tostring()string converts a single piece of data to string type, and returns the data to string because string type is better than all other types

c2 := gojson.Json(json).Get("from").Tostring()
fmt.Println(c2) //en

(4) func (j *Js) Getpath(args... string) *Js gets a value by entering multiple parameters of string. json data 1 must be recursive

c4 := gojson.Json(json).Getpath("trans_result", "src").Tostring()
fmt.Println(c4)  //today

(5) func (j *Js) Arrayindex(i int) string gets the value of the array structure in Json data and returns the corresponding value according to the input num, only the value in [] in {" result ":[" src", "today", "dst", "\u4eca\u5929"]} can be processed

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

(6) func (j * Js) Getkey (key string, i int) * Js this function is for the repeated data in the data, value, use js. data must be [] interface {} type, this is baidu translation return js may be used

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) // It returns trans_result The first 1 In the group src today

(7) func (j *Js) ToArray() (k, d []string) converts json data into key []string [] value []string{} string{} 11, which can only be used up to level 2, not to multiple levels

c9k, c9v := gojson.Json(json1).Get("trans_result").ToArray()
fmt.Println(c9k, c9v) //[src dst src dst] [today Today, tomorrow Tomorrow, ] c3k, c3v := gojson.Json(json).Getindex(1).ToArray()
fmt.Println(c3k, c3v) //    [from] [en]

(8) func (j *Js) Getindex(i int) *Js returns the data in json according to i, which can be searched step by step

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

(9) func (j *Js) StringtoArray() []string returns the data corresponding to result in json :[" src ", "today", "dst", "\ u4u5929"]} data as slice in []string

c11 := gojson.Json(json).Get("result").StringtoArray()
fmt.Println(c11) //[src today dst Today, ]

(10) func (j *Js) Type() for print test, print data type

json := `{"from":"en","to":"zh"}`
c1 := gojson.Json(json) //&{map[from:en to:zh]}

Related articles: