3

Golang Json 处理

 3 years ago
source link: https://www.zhyea.com/2021/05/31/golang-json-process.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

这次记录下golang json处理的几个场景:

  1. 不输出空值
  2. 输出时使用别名
  3. 不输出指定字段

不输出空值

不希望输出空值,可以在注解中使用omitempty,示例代码如:

// Age 年龄
Age int64 `json:"age,omitempty"`

输出时使用别名

依然使用注解,示例:

// Site 网址
Site string `json:"blog"`

不输出指定字段

不输出指定字段有两种方式:

  1. 变量首字母小写,匿名变量
  2. 使用注解,如下:
// Job 工作
Job string `json:"-"`

就是这些了。 示例代码 Json Ignore


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK