golang json marshal:如何省略空嵌套结构
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33447334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
golang json marshal: how to omit empty nested struct
提问by Zhe Hu
As shown in the code above, one can use json:",omitempty"to omit certain fields in a struct to appear in json.
如上面的代码所示,可以使用json:",omitempty"省略结构中的某些字段以出现在 json 中。
For example
例如
type ColorGroup struct {
ID int `json:",omitempty"`
Name string
Colors []string
}
type Total struct {
A ColorGroup`json:",omitempty"`
B string`json:",omitempty"`
}
group := Total{
A: ColorGroup{},
}
In this case, Bwon't show up in json.Marshal(group)
在这种情况下,B不会出现在json.Marshal(group)
However, if
然而,如果
group := Total{
B:"abc",
}
Astill shows up in json.Marshal(group)
A仍然出现在 json.Marshal(group)
{"A":{"Name":"","Colors":null},"B":"abc"}
Question is how do we get only
问题是我们如何获得只有
{"B":"abc"}
EDIT:
After some googling, here is a suggestion use pointer, in other words, turn Totalinto
编辑:经过一些谷歌搜索后,这里有一个建议使用指针,换句话说,Total变成
type Total struct {
A *ColorGroup`json:",omitempty"`
B string`json:",omitempty"`
}
回答by Amit Kumar Gupta
From the documentation:
从文档:
Struct values encode as JSON objects. Each exported struct field becomes a member of the object unless
- the field's tag is "-", or
- the field is empty and its tag specifies the "omitempty" option.
The empty values are false, 0, any nil pointer or interface value, and any array, slice, map, or string of length zero.
结构值编码为 JSON 对象。每个导出的结构字段都成为对象的成员,除非
- 该字段的标签是“-”,或
- 该字段为空,其标签指定了“omitempty”选项。
空值是 false、0、任何 nil 指针或接口值,以及任何长度为零的数组、切片、映射或字符串。
In your declaration of group, it's implicit that group.Awill be the zero value of the ColorGroupstruct type. And notice that zero-values-of-struct-types is not mentioned in that list of things that are considered "empty values".
在您的 声明中group,隐式group.A将是ColorGroup结构类型的零值。请注意,在被视为“空值”的事物列表中没有提到结构类型的零值。
As you found, the workaround for your case is to use a pointer. This will work if you don't specify Ain your declaration of group. If you specify it to be a pointer to a zero-struct, then it will show up again.
如您所见,针对您的情况的解决方法是使用指针。如果您没有A在声明中指定,这将起作用group。如果您将其指定为指向零结构的指针,则它将再次显示。
package main
import (
"encoding/json"
"fmt"
"os"
)
func main() {
type colorGroup struct {
ID int `json:",omitempty"`
Name string
Colors []string
}
type total struct {
A *colorGroup `json:",omitempty"`
B string `json:",omitempty"`
}
groupWithNilA := total{
B: "abc",
}
b, err := json.Marshal(groupWithNilA)
if err != nil {
fmt.Println("error:", err)
}
os.Stderr.Write(b)
println()
groupWithPointerToZeroA := total{
A: &colorGroup{},
B: "abc",
}
b, err = json.Marshal(groupWithPointerToZeroA)
if err != nil {
fmt.Println("error:", err)
}
os.Stderr.Write(b)
}
回答by Purnank Jain
Easy way
简单的方法
type <name> struct {
< varname > < vartype > \`json : -\`
}
Example :
例子 :
type Boy struct {
name string \`json : -\`
}
this way on marshaling namewill not get serialized.
这种编组name方式不会被序列化。

