Go language JSON operation

Source: Internet
Author: User
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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.