JSON operation for Golang

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
package mainimport (    "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"`}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 转map    var dat map[string]interface{}    iferr := json.Unmarshal([]byte(jsonStr), &dat); err == nil {        fmt.Println("==============json str 转map=======================")        fmt.Println(dat)        fmt.Println(dat["host"])    }    //json str 转struct    var config ConfigStruct    iferr := json.Unmarshal([]byte(jsonStr), &config); err == nil {        fmt.Println("================json str 转struct==")        fmt.Println(config)        fmt.Println(config.Host)    }    //json str 转struct(部份字段)    var part Other    iferr := json.Unmarshal([]byte(jsonStr), &part); err == nil {        fmt.Println("================json str 转struct==")        fmt.Println(part)        fmt.Println(part.SerTcpSocketPort)    }    //struct 到json str    if b, err := json.Marshal(config); err == nil {        fmt.Println("================struct 到json str==")        fmt.Println(string(b))    }    //map 到json str    fmt.Println("================map 到json str=====================")    enc := json.NewEncoder(os.Stdout)    enc.Encode(dat)    //array 到 json str    arr := []string{"hello""apple""python""golang""base""peach""pear"}    lang, err := json.Marshal(arr)    iferr == nil {        fmt.Println("================array 到 json str==")        fmt.Println(string(lang))    }    //json 到 []string    var wo []string    if err := json.Unmarshal(lang, &wo); err == nil {        fmt.Println("================json 到 []string==")        fmt.Println(wo)    }}

from:http://my.oschina.net/golang/blog/210012

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.