分享
GO JsonStr 2 obj
痞子汤 · · 1627 次点击 · · 开始浏览这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。
//project main.go
package main
import (
"encoding/json"
"fmt"
)
func main() {
fmt.Println(help())
b := []byte(`{
"Title": "Go语言编程",
"Authors": ["XuShiwei", "HughLv", "Pandaman", "GuaguaSong", "HanTuo", "BertYuan", "XuDaoli"],
"Publisher": "ituring.com.cn",
"IsPublished": true,
"Price": 9.99,
"Sales": 1000000
}`)
var r interface{}
err := json.Unmarshal(b, &r)
fmt.Println("r = ", r, "err = ", err, "\n")
gobook, ok := r.(map[string]interface{})
if ok {
for k, v := range gobook {
switch v2 := v.(type) {
case string:
fmt.Println(k, "is string", v2)
case int:
fmt.Println(k, "is int", v2)
case bool:
fmt.Println(k, "is bool", v2)
case []interface{}:
fmt.Println(k, "is an array:")
for i, iv := range v2 {
fmt.Println(i, iv)
}
default:
fmt.Println(k, "is another type not handle yet")
}
}
}
}
func help() string {
return `
Go内建这样灵活的类型系统,向我们传达了一个很有价值的信息:空接口是通用类型。如
果要解码一段未知结构的JSON,只需将这段JSON数据解码输出到一个空接口即可。在解码JSON
数据的过程中, JSON数据里边的元素类型将做如下转换:
JSON中的布尔值将会转换为Go中的bool类型;
数值会被转换为Go中的float64类型;
字符串转换后还是string类型;
JSON数组会转换为[]interface{}类型;
JSON对象会转换为map[string]interface{}类型;
null值会转换为nil
`
}
输出:
Go内建这样灵活的类型系统,向我们传达了一个很有价值的信息:空接口是通用类型。如
果要解码一段未知结构的JSON,只需将这段JSON数据解码输出到一个空接口即可。在解码JSON
数据的过程中, JSON数据里边的元素类型将做如下转换:
JSON中的布尔值将会转换为Go中的bool类型;
数值会被转换为Go中的float64类型;
字符串转换后还是string类型;
JSON数组会转换为[]interface{}类型;
JSON对象会转换为map[string]interface{}类型;
null值会转换为nil
r = map[IsPublished:true Price:9.99 Sales:1e+06 Title:Go语言编程 Authors:[XuShiwei HughLv Pandaman GuaguaSong HanTuo BertYuan XuDaoli] Publisher:ituring.com.cn] err = <nil>
Title is string Go语言编程
Authors is an array:
0 XuShiwei
1 HughLv
2 Pandaman
3 GuaguaSong
4 HanTuo
5 BertYuan
6 XuDaoli
Publisher is string ituring.com.cn
IsPublished is bool true
Price is another type not handle yet
Sales is another type not handle yet
有疑问加站长微信联系(非本文作者)
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
关注微信1627 次点击
上一篇:Go struct 2 json
下一篇:谈谈一些关于mgo的用法
添加一条新回复
(您需要 后才能回复 没有账号 ?)
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码` - 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传
收入到我管理的专栏 新建专栏
//project main.go
package main
import (
"encoding/json"
"fmt"
)
func main() {
fmt.Println(help())
b := []byte(`{
"Title": "Go语言编程",
"Authors": ["XuShiwei", "HughLv", "Pandaman", "GuaguaSong", "HanTuo", "BertYuan", "XuDaoli"],
"Publisher": "ituring.com.cn",
"IsPublished": true,
"Price": 9.99,
"Sales": 1000000
}`)
var r interface{}
err := json.Unmarshal(b, &r)
fmt.Println("r = ", r, "err = ", err, "\n")
gobook, ok := r.(map[string]interface{})
if ok {
for k, v := range gobook {
switch v2 := v.(type) {
case string:
fmt.Println(k, "is string", v2)
case int:
fmt.Println(k, "is int", v2)
case bool:
fmt.Println(k, "is bool", v2)
case []interface{}:
fmt.Println(k, "is an array:")
for i, iv := range v2 {
fmt.Println(i, iv)
}
default:
fmt.Println(k, "is another type not handle yet")
}
}
}
}
func help() string {
return `
Go内建这样灵活的类型系统,向我们传达了一个很有价值的信息:空接口是通用类型。如
果要解码一段未知结构的JSON,只需将这段JSON数据解码输出到一个空接口即可。在解码JSON
数据的过程中, JSON数据里边的元素类型将做如下转换:
JSON中的布尔值将会转换为Go中的bool类型;
数值会被转换为Go中的float64类型;
字符串转换后还是string类型;
JSON数组会转换为[]interface{}类型;
JSON对象会转换为map[string]interface{}类型;
null值会转换为nil
`
}
输出:
Go内建这样灵活的类型系统,向我们传达了一个很有价值的信息:空接口是通用类型。如
果要解码一段未知结构的JSON,只需将这段JSON数据解码输出到一个空接口即可。在解码JSON
数据的过程中, JSON数据里边的元素类型将做如下转换:
JSON中的布尔值将会转换为Go中的bool类型;
数值会被转换为Go中的float64类型;
字符串转换后还是string类型;
JSON数组会转换为[]interface{}类型;
JSON对象会转换为map[string]interface{}类型;
null值会转换为nil
r = map[IsPublished:true Price:9.99 Sales:1e+06 Title:Go语言编程 Authors:[XuShiwei HughLv Pandaman GuaguaSong HanTuo BertYuan XuDaoli] Publisher:ituring.com.cn] err = <nil>
Title is string Go语言编程
Authors is an array:
0 XuShiwei
1 HughLv
2 Pandaman
3 GuaguaSong
4 HanTuo
5 BertYuan
6 XuDaoli
Publisher is string ituring.com.cn
IsPublished is bool true
Price is another type not handle yet
Sales is another type not handle yet