ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
[TOC] # 编码 ## 结构体生成json 使用json.Marshal()函数可以对一组数据进行json格式的编码. json.Marshal()函数的声明 ~~~ func Marshal(v interface{}) ([] byte, error) ~~~ 还有一个格式化输出 ~~~ //MarshalIndent很像Marshal, 只是用缩进对输出进行格式化 func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) ~~~ **Marshal** ~~~ //成员变量名首字母必须大写 type IT struct { Company string Subjects []string IsOk bool Price float64 } func main() { //定义一个结构体变量,同时初始化 s := IT{"abc", []string{"go", "cpp", "test"}, true, 66.66} //编码,根据内容生成json文本 buf, err := json.Marshal(s) if err != nil { fmt.Println("err = ", err) return } //要转成字符串 //{"Company":"abc","Subjects":["go","cpp","test"],"IsOk":true,"Price":66.66} fmt.Println(string(buf)) } ~~~ **MarshalIndent** ~~~ type IT struct { Company string Subjects []string IsOk bool Price float64 } func main() { //定义一个结构体变量,同时初始化 s := IT{"abc", []string{"go", "cpp", "test"}, true, 66.66} //编码,第3个参数是缩进 buf, err := json.MarshalIndent(s, "", " ") if err != nil { fmt.Println("err = ", err) return } //要转成字符串 fmt.Println(string(buf)) } ~~~ ## 二次编码 Go 语言中数据结构和 JSON 类型的对应关系如下表 ![](https://box.kancloud.cn/bfc27cb31c3152a009b869b9a6e7aa49_1890x756.png) 有些时候,我们在序列化或者反序列化的时候,可能结构体类型和需要的类型不一致,这个时候可以指定,支持string,number和boolean ~~~ //成员变量名首字母必须大写 type IT struct { Company string `json:"company"` //改名字 Subjects []string `json:"sub"` IsOk bool `json:",string"` //转成字符串 Price float64 `json:"-"` //这个不会输出 } func main() { //定义一个结构体变量,同时初始化 s := IT{"abc", []string{"go", "cpp", "test"}, true, 66.66} //编码,根据内容生成json文本 buf, err := json.Marshal(s) if err != nil { fmt.Println("err = ", err) return } //要转成字符串 //{"company":"abc","sub":["go","cpp","test"],"IsOk":"true"} fmt.Println(string(buf)) } ~~~ ~~~ `json:"-"` ~~~ 表示不进行序列化` ~~~ type Product struct { Name string `json:"name"` ProductID int64 `json:"-"` // 表示不进行序列化 Number int `json:"number"` Price float64 `json:"price"` IsOnSale bool `json:"is_on_sale,string"` } // 序列化过后,可以看见 {"name":"Xiao mi 6","number":10000,"price":2499,"is_on_sale":"false"} ~~~ * `omitempty`:当字段为空(默认值)时,不要解析这个字段。比如 false、0、nil、长度为 0 的 array,map,slice,string ~~~ import ( "encoding/json" "fmt" ) // Product _ type Product struct { Name string `json:"name"` ProductID int64 `json:"product_id,omitempty"` Number int `json:"number"` Price float64 `json:"price"` IsOnSale bool `json:"is_on_sale,omitempty"` } func main() { p := &Product{} p.Name = "Xiao mi 6" p.IsOnSale = false p.Number = 10000 p.Price = 2499.00 p.ProductID = 0 data, _ := json.Marshal(p) fmt.Println(string(data)) } // 结果 {"name":"Xiao mi 6","number":10000,"price":2499} ~~~ ## 通过map生成json ~~~ func main() { //创建个map m := make(map[string]interface{}, 4) m["company"] = "abc" m["sub"] = []string{"go", "c++", "test"} m["isok"] = true m["price"] = 6.66 //编码成json res, err:= json.MarshalIndent(m, "", " ") if err != nil { fmt.Println(err) return } fmt.Println(string(res)) } ~~~ # 解析 ## 解析到结构体 ~~~ //成员变量名首字母必须大写 type IT struct { Company string `json:"company"` //改名字 Subjects []string `json:"sub"` IsOk bool `json:",string"` //转成字符串 Price float64 `json:"-"` //这个不会输出 } func main() { jsonBuf := ` {"company":"abc","sub":["go","cpp","test"],"IsOk":"true"} ` var tmp IT //参数一要切片[]byte,第二个参数要地址传递 res := json.Unmarshal([]byte(jsonBuf), &tmp) if res != nil { fmt.Println(res) return } fmt.Println(tmp) //详细打印 fmt.Printf("%+v\n", tmp) } ~~~ ## 解析到map ~~~ func main() { jsonBuf := ` {"company":"abc","sub":["go","cpp","test"],"IsOk":"true"} ` m := make(map[string]interface{}, 4) //参数一要切片[]byte,第二个参数要地址传递 res := json.Unmarshal([]byte(jsonBuf), &m) if res != nil { fmt.Println(res) return } //详细打印 fmt.Printf("%+v\n", m) } ~~~ **类型断言** 因为字符串不能直接接收 ~~~ func main() { jsonBuf := ` {"company":"abc","sub":["go","cpp","test"],"IsOk":"true"} ` m := make(map[string]interface{}, 4) //参数一要切片[]byte,第二个参数要地址传递 res := json.Unmarshal([]byte(jsonBuf), &m) if res != nil { fmt.Println(res) return } //str := string(m["company"])// 错误不能转换 //fmt.Println(str) /** IsOk ===> true company ===> abc sub ===> [go cpp test] */ for key, value := range m { fmt.Printf("%v ===> %v\n", key, value) switch data := value.(type) { case []interface{}: //map[sub] 值是string, value是: [go cpp test] fmt.Printf("map[%s] 值是string, value是: %s\n", key, data) } } } ~~~ # 和 stream 中 JSON 打交道 上面所有的 JSON 数据来源都是预先定义的`[]byte`缓存,在很多时候,如果能读取/写入其他地方的数据就好了。`encoding/json`库中有两个专门处理这个事情的结构:`Decoder`和`Encoder`: ~~~ // Decoder 从 r io.Reader 中读取数据,`Decode(v interface{})` 方法把数据转换成对应的数据结构 func NewDecoder(r io.Reader) *Decoder // Encoder 的 `Encode(v interface{})` 把数据结构转换成对应的 JSON 数据,然后写入到 w io.Writer 中 func NewEncoder(w io.Writer) *Encoder ~~~ 下面的例子就是从标准输入流中读取数据,解析成数据结构,删除所有键不是`Name`的字段,然后再 encode 成 JSON 数据,打印到标准输出。 ~~~ package main import ( "encoding/json" "log" "os" ) func main() { dec := json.NewDecoder(os.Stdin) enc := json.NewEncoder(os.Stdout) for { var v map[string]interface{} if err := dec.Decode(&v); err != nil { log.Println(err) return } for k := range v { if k != "Name" { delete(v, k) } } if err := enc.Encode(&v); err != nil { log.Println(err) } } } ~~~ # 反序列化的\[\]和nil ~~~ package main import ( "encoding/json" "log" ) type JS struct { A []string } func main() { v1 := &JS{} v2 := &JS{A: []string{}} o1, err := json.Marshal(&v1) log.Println(string(o1), err) o2, err2 := json.Marshal(&v2) log.Println(string(o2), err2) } ~~~ 输出 ~~~ 2019/01/07 18:13:26 {"A":null} <nil> 2019/01/07 18:13:26 {"A":[]} <nil> ~~~