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)}
}