golang json marshal:如何省略空的嵌套结构 [英] golang json marshal: how to omit empty nested struct

查看:790
本文介绍了golang json marshal:如何省略空的嵌套结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

去操场



如上面代码所示,可以使用 json:,omitempty来省略结构中的某些字段以显示在json中。



例如

  type ColorGroup struct {
ID int`json:,omitempty`
名称字符串
颜色[]字符串
}
类型总计结构{
ColorGroup`json:,omitempty
B string`json:,omitempty`
}
组合:=合计{
A:ColorGroup {},

}

在这种情况下, B 不会显示在 json.Marshal(group)



然而,如果

  group:=合计{
B:abc,

}

A 仍然显示在 json.Marshal(group)

  {A:{Name:,Colors:null},B: abc} 

问题是我们如何得到

  {B:abc} 

编辑:
一些谷歌搜索后,这里是一个建议使用指针,换句话说,将 Total 转换为

 类型总结构{
A * ColorGroup`json:,omitempty`
B string`json:,omitempty `
}


解决方案

From 文档


结构值编码为JSON对象。除非


  • 字段标记为 - ,或者

  • ,否则每个导出的结构字段都将成为对象的成员
  • 该字段为空,其标签指定omitempty选项。


空值为假,0,任何零指针或接口值以及任何数组,切片,映射或长度为零的字符串。

在声明 group ,隐含的是 group.A 将是 ColorGroup 结构类型。请注意,在被视为空值的事物列表中没有提及零值结构类型。



正如您发现的那样,解决方法是你的情况是使用一个指针。如果您在 group 的声明中未指定 A ,这将起作用。如果你指定它是一个零结构指针,它会再次出现。



游乐场链接

 包主

$ import
encoding / json
fmt
os


func main(){
type colorGroup struct {
ID int`json:,omitempty`
名称字符串
颜色[]字符串
}
类型总结构{
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.Mar shal(groupWithPointerToZeroA)
if err!= nil {
fmt.Println(error:,err)
}
os.Stderr.Write(b)
}


go playground

As shown in the code above, one can use json:",omitempty" to omit certain fields in a struct to appear in 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, B won't show up in json.Marshal(group)

However, if

group := Total{
       B:"abc",

}

A still shows up in 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 Total into

type Total struct {
    A *ColorGroup`json:",omitempty"`
    B string`json:",omitempty"`
}

解决方案

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.

In your declaration of group, it's implicit that group.A will be the zero value of the ColorGroup struct type. And notice that zero-values-of-struct-types is not mentioned in that list of things that are considered "empty values".

As you found, the workaround for your case is to use a pointer. This will work if you don't specify A in your declaration of group. If you specify it to be a pointer to a zero-struct, then it will show up again.

playground link:

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)
}

这篇关于golang json marshal:如何省略空的嵌套结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆