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

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

最新文章

  1. Gradle多渠道打包
  2. jquery添加光棒效果的各种方式以及简单动画复杂动画
  3. Display: table-cell实现img、文字垂直居中
  4. WIN7远程桌面连接方法!
  5. [Editor]Unity Editor类常用方法
  6. 使用CSS修改HTML5 input placeholder颜色
  7. CentOS7 安装Docker报错
  8. C++学习(四)
  9. ARM流水线(pipeline)
  10. JavaScript 学习笔记 - LocalStorage
  11. zabbix灵活使用userparameters
  12. GIS前端将选中的图形输出为Shapfile文件
  13. 一起学爬虫——使用xpath库爬取猫眼电影国内票房榜
  14. css学习_cs3s旋转的图片
  15. javascript中的add(1)(2)(3)(4)是怎么实现的
  16. Android Studio 导致C盘过大
  17. POJ 1733 Parity game 【带权并查集】+【离散化】
  18. MD5生成
  19. MYSQL之 GroupCommit
  20. php mysql 查询判断周几

热门文章

  1. insert NULL into mysql
  2. sqlserver字符串拆分函数
  3. C++ 0x std::async 的应用
  4. 1Java语言概述——重拾Java
  5. 疯狂JAVA——第八章 java集合
  6. eclipse新建maven项目出现红叉解决办法
  7. org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[]
  8. 最短路径Dijkstra算法(邻接矩阵)
  9. [leetcode]277. Find the Celebrity 找名人
  10. JSF控件的immediate属性和页面生命周期