Recently wrote a go JSON module that has been posted to GitHub. I hope you'll give us some advice.

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

GitHub Address Https://github.com/hljdxjiang/gojson

Code usage examples

Package Mainimport ("FMT" "Github.com/hljdxjiang/gojson") func main () {jsonobjecttest () jsonarraytest ()}func Jsonobjecttest () {jsonstring: = ' {' a1 ': ' A1 '} ' Jo, err: = Gojson. Jsonobject (jsonstring) if err! = Nil {fmt. PRINTLN (Err)} Jo. Setkey ("A2", "A2") fmt. Println (Jo. Encode ())//{"A1": "A1", "A2": "A2"}//update a1.value Jo. Setkey ("A1", "A3") fmt. Println (Jo. Encode ())//{"A1": "A3", "A2": "A2"}//new a JSON without item Njo: = Gojson. Newjsonobject () Njo. Setkey ("Ad1", "Ad1")//{"ad1": "Ad1"} fmt. Println (Njo. Encode ())//add another jsonobject Njo. Appendobject (*jo) fmt. Println (Njo. Encode ())//{"Ad1": "Ad1", "A1": "A3", "A2": "A2"} Njo. Setkey ("Addjson", *jo) fmt. Println (Njo. Encode ())//{"Ad1": "Ad1", "A1": "A3", "A2": "A2", "Addjson": {"a1": "A3", "A2": "A2"}} FMT. Println (Njo. Getvaluebyindex (1). Asstring (""))//a2 for _, val: = Range Njo. Getkeys () {fmt. Println (Njo. GetValue (Val). Asstring (")")} FMT. Println (Jo. GetIndex ("A2 ")) Fmt. Println (Jo. Getvaluebyindex (1). Asstring ()}} func Jsonarraytest () {jsonarrstring: = ' [' AA ', ' BB '] ' ja, err: = Gojson. Jsonarray (jsonarrstring) if err! = Nil {fmt. PRINTLN (Err)} ja. AddItem ("CC") jsonstring: = ' {"a1": "A1"} ' Jo, err: = Gojson. Jsonobject (jsonstring) if err! = Nil {fmt. PRINTLN (Err)} ja. AddItem (*jo) fmt. Println (ja. Encode ())//["AA", "BB", "cc", {"a1": "A1"}] Nja: = Gojson. Newjsonarray () Nja. AddItem ("FF") Nja. Append (*ja) fmt. Println (Nja. Encode ())//{"Ad1": "Ad1", "A1": "A3", "A2": "A2", "Addjson": {"a1": "A3", "A2": "A2"}}
463 reads  
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.