JSON operation for Golang

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

Package main Import ("Encoding/json" "FMT" "OS") type configstruct struct {Host string ' JSON:"Host"' Portint' JSON: ' Port '' Analyticsfile string ' JSON:"Analytics_file"' Staticfileversionint' JSON: ' Static_file_version '' Staticdir string ' JSON:"Static_dir"' TemplatesDir string ' JSON:"Templates_dir"' Sertcpsockethost string ' JSON:"Sertcpsockethost"' Sertcpsocketportint' JSON: ' Sertcpsocketport '' Fruits []string ' JSON:"Fruits"'} type other struct {sertcpsockethost string ' JSON:"Sertcpsockethost"' Sertcpsocketportint' JSON: ' Sertcpsocketport '' Fruits []string ' JSON:"Fruits"'} func main () {jsonstr:= ' {' host ': ' http://localhost:9090 ', ' Port ': 9090, ' analytics_file ': ', ' static_file_version ': 1, ' static_dir ': ' e:/ project/gotest/src/"," Templates_dir ":" e:/project/gotest/src/templates/"," Sertcpsockethost ":": 12340 "," Sertcpsocketport ": 12340," fruits ": [" apple "," peach "]}`     //JSON str goto map    vardat map[string]interface{}ifERR: = json. Unmarshal ([]byte(JSONSTR), &dat); Err = =Nil {fmt. Println ("==============json str Turn map=======================") fmt. PRINTLN (DAT) fmt. Println (dat["Host"])    }     //json str transfer struct    varconfig configstructifERR: = json. Unmarshal ([]byte(JSONSTR), &config); Err = =Nil {fmt. Println ("================json str Turn struct==") fmt. Println (config) fmt. Println (config. Host)}//json str to struct (partial field)    var Part OtherifERR: = json. Unmarshal ([]byte(JSONSTR), &part); Err = =Nil {fmt. Println ("================json str Turn struct==") fmt. Println (part) FMT. Println (part. Sertcpsocketport)}//struct to JSON str    ifB, err: = json. Marshal (config); Err = =Nil {fmt. Println ("================struct to JSON str==") fmt. Println (string (b))}//map to JSON strFmt. Println ("================map to JSON str=====================") ENC:=JSON. Newencoder (OS. Stdout) Enc. Encode (DAT)//array to json strArr: = []string{"Hello", "apple", "Python", "Golang", "base", "peach", "pear"} lang, err:=JSON. Marshal (arr)ifErr = =Nil {fmt. Println ("================array to JSON str==") fmt. Println (String (lang))}//json to []string    varwo []stringifERR: = json. Unmarshal (lang, &wo); Err = =Nil {fmt. Println ("================json to []string==") fmt. Println (wo)}}

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.