golang的json操作

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"`
}
 
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{}
    if err := 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
    if err := 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
    if err := 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)
    if err == 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)
    }
}

参考第三方json库 simplejson 的用法:http://blog.haohtml.com/archives/16851

转自:http://www.cnblogs.com/go-ios/p/3906251.html

29 thoughts on “golang的json操作

  1. Pingback: Best Fitness Club In Chilliwack

  2. Pingback: 온라인카지노

  3. Pingback: gift ideas for Bat Mitzvah girl

  4. Pingback: ลิเวอร์พูล

  5. Pingback: surfside supply co s s overdyed slub tee navy blazer

  6. Pingback: Samsara Market

  7. Pingback: Darknet Drogen

  8. Pingback: cross borders all weather

  9. Pingback: 카지노

  10. Pingback: 사설토토

  11. Pingback: 럭스112

  12. Pingback: 토토사이트

  13. Pingback: wrap your boards

  14. Pingback: https://bizhub24.pl/forum/

  15. Pingback: addiction

  16. Pingback: 카지노사이트

  17. Pingback: Institutional Repository

  18. Pingback: 바카라 사이트 신고

  19. Pingback: 조커온라인카지노

  20. Pingback: Cannabis Equipment Loans

  21. Pingback: opinie

  22. Pingback: daftar poker online

  23. Pingback: Juul Flavors

  24. Pingback: http://162.0.224.134/

  25. Pingback: Rick Simpson Oil for sale

  26. Pingback: Cannabis Oil for Sale

  27. Pingback: Darknet

  28. Pingback: tylenol generic

  29. Pingback: bahis siteleri

Comments are closed.