分享
golang的json操作
sunshine-anycall · · 2730 次点击 · · 开始浏览这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。
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 转mapvar 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 转structvar config ConfigStructif 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 Otherif err := json.Unmarshal([]byte(jsonStr), &part); err == nil {fmt.Println("================json str 转struct==")fmt.Println(part)fmt.Println(part.SerTcpSocketPort)}//struct 到json strif b, err := json.Marshal(config); err == nil {fmt.Println("================struct 到json str==")fmt.Println(string(b))}//map 到json strfmt.Println("================map 到json str=====================")enc := json.NewEncoder(os.Stdout)enc.Encode(dat)//array 到 json strarr := []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 到 []stringvar wo []stringif err := json.Unmarshal(lang, &wo); err == nil {fmt.Println("================json 到 []string==")fmt.Println(wo)}}from:http://my.oschina.net/golang/blog/210012
有疑问加站长微信联系(非本文作者)
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
关注微信2730 次点击
添加一条新回复
(您需要 后才能回复 没有账号 ?)
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码` - 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传
收入到我管理的专栏 新建专栏
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 转mapvar 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 转structvar config ConfigStructif 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 Otherif err := json.Unmarshal([]byte(jsonStr), &part); err == nil {fmt.Println("================json str 转struct==")fmt.Println(part)fmt.Println(part.SerTcpSocketPort)}//struct 到json strif b, err := json.Marshal(config); err == nil {fmt.Println("================struct 到json str==")fmt.Println(string(b))}//map 到json strfmt.Println("================map 到json str=====================")enc := json.NewEncoder(os.Stdout)enc.Encode(dat)//array 到 json strarr := []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 到 []stringvar wo []stringif err := json.Unmarshal(lang, &wo); err == nil {fmt.Println("================json 到 []string==")fmt.Println(wo)}}from:http://my.oschina.net/golang/blog/210012