Go operation JSON, note the nesting of JSON, array

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 ' ' Port              int      ' JSON: ' Port ' ' analyticsfile     string   ' json: ' Analytics_ File "' Staticfileversion int      ' JSON:" Static_file_version "' Staticdir         string   ' JSON:" Static_dir "' TemplatesDir      string   ' json: ' Templates_dir ' ' sertcpsockethost  string   ' json: ' Sertcpsockethost ' ' Sertcpsocketport  int      ' JSON: ' Sertcpsocketport ' ' Fruits            []string ' JSON: ' Fruits '

}

Type other struct {

Sertcpsockethost string   ' json: ' sertcpsockethost ' ' sertcpsocketport int      ' JSON: ' Sertcpsocketport ' ' Fruits           []string ' JSON: ' Fruits '

}

Type Object []interface{}

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 "]} ' Jsonstr = ' {" Accesstoken ":" 507b5e08ee444dck887b66bd08672905 " , "Clienttoken": "64e3a5415bfe405d9485f1jf2ea5c68e", "Selectedprofile": {"id": "selid", "name": "Bluek404"}, " Availableprofiles ": [{" id ":" Test Ava "," name ":" Bluek404 "}]} '//json str to Mapvar dat map[string]interface{}if err: = json. Unmarshal ([]byte (JSONSTR), &dat); Err = = Nil {fmt. PRINTLN ("==============json str turn map=======================") fmt. PRINTLN (DAT) maptmp: = dat["Selectedprofile"]. (Map[string]interface {}) fmt. PRINTLN (maptmp["id"])/* var dat2 map[string]interface{} If err: = json. Unmarshal ([]byte (JSONSTR), &DAT2); Err = = Nil {fmt. Println (dat2["FirstName"])} */MAPTMP2: = (dat["Availableprofiles"]. ( []INTERFACe {})) [0]. (Map[string]interface {})//maptmp3: = Maptmp2[0]. (Map[string]interface {}) fmt. PRINTLN (maptmp2["id"])}//json str to structvar config configstructif err: = 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 otherif err: = json. Unmarshal ([]byte (JSONSTR), &part); Err = = Nil {fmt. PRINTLN ("================json str turn struct==") fmt. Println (part) FMT. Println (part. Sertcpsocketport)}//struct to JSON Strif B, 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: = JS On. Marshal (arr) if Err = = Nil {fmt. Println ("================arrAy to JSON str== ") fmt. Println (String (lang))}//json to []stringvar wo []stringif err: = 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.