一、介绍
官网文档 https://pkg.go.dev/encoding/json 有介绍
- omitempty如果字段的值为空值(定义为 false、0、nil 指针、nil 接口值以及任何空数组、切片、映射或字符串),该选项在编码期间疏忽该字段
- "-"标签在编码和解码时总是省略字段
二、omitempty空值省略
2.1 json在构造体重的写法
咱们常常看到如下的构造体中,用tag标注了解析json的格局
首先咱们初始化一个空构造体Product
package jsonimport ( "encoding/json" "testing")type Product struct { OrderId string `json:"order_id"` CommentS []Comment `json:"comment_s"`}type Comment struct { Id int Content string}func TestJson(t *testing.T) { p := Product{} s, _ := json.Marshal(p) t.Logf("%+v", p) t.Log(string(s))}
输入如下:
{OrderId: CommentS:[]} //p构造体{"order_id":"","comment_s":null} //p的json对象
咱们发现空构造是有值的!!!
和咱们料想的,一个空构造,对应一个空json {}有点不一样。
是因为go中的构造体,都会有一个默认值的空值。
如果空值省略??
用 omitempty,代码批改如下:
package jsonimport ( "encoding/json" "testing")type Product struct { OrderId string `json:"order_id,omitempty"` CommentS []Comment `json:"comment_s,omitempty"`}type Comment struct { Id int Content string}func TestJson(t *testing.T) { p := Product{} s, _ := json.Marshal(p) t.Logf("%+v", p) t.Log(string(s))}
输入如下:
{OrderId: CommentS:[]} //p空构造体{} //p的空json对象
如果构造体外面蕴含构造体呢?如何省略,示例如下:
package jsonimport ( "encoding/json" "testing")type Product struct { OrderId string `json:"order_id,omitempty"` CommentS []Comment `json:"comment_s,omitempty"` OneComment Comment `json:"one_comment,omitempty"`}type Comment struct { Id int `json:"id"` Content string `json:"content"`}func TestJson(t *testing.T) { p := Product{} s, _ := json.Marshal(p) t.Logf("%+v", p) t.Log(string(s))}
输入如下:
{OrderId: CommentS:[] OneComment:{Id:0 Content:}} //p空构造体{"one_comment":{"id":0,"content":""}} //p的空json对象
咱们发现json不再是 {}了 one_comment 还是展现了,为啥?
因为 OneComment 构造体的零值,是一个 Comment 构造体
type Comment struct { Id int `json:"id"` Content string `json:"content"`}
而Comment的Id,和Content并没有零值省略。咱们加上omitempty
package jsonimport ( "encoding/json" "testing")type Product struct { OrderId string `json:"order_id,omitempty"` CommentS []Comment `json:"comment_s,omitempty"` OneComment Comment `json:"one_comment,omitempty"`}type Comment struct { Id int `json:"id,omitempty"` Content string `json:"content,omitempty"`}func TestJson(t *testing.T) { p := Product{} s, _ := json.Marshal(p) t.Logf("%+v", p) t.Log(string(s))}
输入如下:
{OrderId: CommentS:[] OneComment:{Id:0 Content:}}{"one_comment":{}}
咱们发现 并不是{},即便全副加上omitempty,蕴含的
OneComment Comment `json:"one_comment,omitempty"`
并不是{},而是"one_comment":{}
还记得官网文档吗,并没有空构造体
omitempty如果字段的值为空值(定义为 false、0、nil指针、nil接口值以及空数组、切片、map或字符串),该选项在编码期间疏忽该字段
此时咱们改成 空构造变成空指针 OneComment Comment*
OneComment *Comment `json:"one_comment,omitempty"`
变成
package jsonimport ( "encoding/json" "testing")type Product struct { OrderId string `json:"order_id,omitempty"` CommentS []Comment `json:"comment_s,omitempty"` OneComment *Comment `json:"one_comment,omitempty"`}type Comment struct { Id int `json:"id,omitempty"` Content string `json:"content,omitempty"`}func TestJson(t *testing.T) { p := Product{} s, _ := json.Marshal(p) t.Logf("%+v", p) t.Log(string(s))}
输入如下:
{OrderId: CommentS:[] OneComment:<nil>}{} //p的空json