This is a creation in Article, where the information may have evolved or changed.
You can use the "Encoding/json" in the official go language package to work with JSON data, in detail with examples of JSON packages.
There is also a simpler and more straightforward way to use the Github.com/bitly/go-simplejson package, which encapsulates the Unmarshal and Marshal methods in Encoding/json and provides an easier interface.
Address: Https://github.com/bitly/go-simplejson
Document: Http://godoc.org/github.com/bitly/go-simplejson
Specific usage can refer to the test code in the package: Simplejson_test.go, very convenient.
Package Mainimport ("FMT" "Github.com/bitly/go-simplejson" "StrConv") func main () {js, err: = Simplejson. Newjson ([]byte (' {"" test ": {" String_array ": [" asdf "," GHJK "," ZXCV "]," array ": [1," 2 ", 3]," arraywithsubs ": [{" Subkeyone " : 1},{"Subkeytwo": 2, "Subkeythree": 3}], "int": Ten, "float": 5.150, "Bignum": 9223372036854775807, "string": "Simplejson" , "bool": True}} ')) if err! = Nil {panic ("JSON format error")}s, err: = JS. Get ("Test"). Get ("string"). String () if err! = Nil {fmt. Println ("Decode Error:get int failed!") Return}fmt. PRINTLN (s)//check if key exists _, OK: = js. Checkget ("Missing_key") if OK {fmt. Println ("Key Missing_key exists")} else {fmt. Println ("Key Missing_key NOT EXISTS")}arr, err: = JS. Get ("Test"). Get ("array"). Array () if err! = Nil {fmt. Println ("Decode Error:get array failed!") Return}for _, V: = Range arr {var IV intswitch v. (type) {case float64:iv = Int (v. (float64)) fmt. Println (iv) Case STRING:IV, _ = StrConv. Atoi (V. (string)) Fmt. Println (iv)}}}
The
One need to be aware of is that the int value is unmarshal by the JSON, and the type is float64. The code for
Simplejson is very exciting and recommended to read.