golang的json操作[转]
2024-10-16 15:56:03
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
最新文章
- Gradle多渠道打包
- jquery添加光棒效果的各种方式以及简单动画复杂动画
- Display: table-cell实现img、文字垂直居中
- WIN7远程桌面连接方法!
- [Editor]Unity Editor类常用方法
- 使用CSS修改HTML5 input placeholder颜色
- CentOS7 安装Docker报错
- C++学习(四)
- ARM流水线(pipeline)
- JavaScript 学习笔记 - LocalStorage
- zabbix灵活使用userparameters
- GIS前端将选中的图形输出为Shapfile文件
- 一起学爬虫——使用xpath库爬取猫眼电影国内票房榜
- css学习_cs3s旋转的图片
- javascript中的add(1)(2)(3)(4)是怎么实现的
- Android Studio 导致C盘过大
- POJ 1733 Parity game 【带权并查集】+【离散化】
- MD5生成
- MYSQL之 GroupCommit
- php mysql 查询判断周几
热门文章
- insert NULL into mysql
- sqlserver字符串拆分函数
- C++ 0x std::async 的应用
- 1Java语言概述——重拾Java
- 疯狂JAVA——第八章 java集合
- eclipse新建maven项目出现红叉解决办法
- org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[]
- 最短路径Dijkstra算法(邻接矩阵)
- [leetcode]277. Find the Celebrity 找名人
- JSF控件的immediate属性和页面生命周期