golang中json的omitempty使用操作
作者:wilsonyx 时间:2024-05-13 09:06:44
我就废话不多说了,大家还是直接看代码吧~
package main
import (
"encoding/json"
"fmt"
)
type Project struct {
Name string `json:"name"`
Url string `json:"url"`
Docs string `json:"docs,omitempty"`
}
func main() {
p1 := Project{
Name:"hello name",
Url:"https://blog.csdn.net/qq_30505673",
}
data, err := json.Marshal(p1)
if err != nil {
panic(err)
}
// Docs定义为omitempty所以不会出现Docs的字段
fmt.Printf("%s\n", data)
p2 := Project{
Name:"lovego",
Url:"https://blog.csdn.net/qq_30505673",
Docs:"https://blog.csdn.net/qq_30505673",
}
data2, err := json.Marshal(p2)
if err != nil {
panic(err)
}
//打印出所有的字段
fmt.Printf("%s\n", data2)
}
如果没有omitempty,该字段是会显示的。
补充:golang omitempty实现嵌套结构体的省略输出
golang在处理json转换时,对于标签omitempty定义的field,如果给它赋得值恰好等于空值(比如:false、0、""、nil指针、nil接口、长度为0的数组、切片、映射),则在转为json之后不会输出这个field。
那么,针对结构体中嵌套结构体,如果嵌套结构体为空,是否也会忽略?如果要忽略空结构体输出,怎么处理?
情况一:匿名结构体:使用omitempty修饰该匿名结构体中的字段,那么当为空时不会输出
type Book struct{
Name string `json:"name"`
Price float32 `json:"price"`
Desc string `json:"desc,omitempty"`
Author //匿名结构体
}
type Author struct {
Gender int `json:"gender,omitempty"`
Age int `json:"age,omitempty"`
}
func main() {
var book Book
book.Name = "testBook"
bookByte,_:=json.Marshal(book)
fmt.Printf("%s\n", string(bookByte))
}
输出:
{"name":"testBook","price":0}
情况二:非匿名结构体
type Book struct{
Name string `json:"name"`
Price float32 `json:"price"`
Desc string `json:"desc,omitempty"`
Author Author `json:"author,omitempty"`
}
type Author struct {
Gender int `json:"gender,omitempty"`
Age int `json:"age,omitempty"`
}
func main() {
var book Book
book.Name = "testBook"
bookByte,_:=json.Marshal(book)
fmt.Printf("%s\n", string(bookByte))
}
输出:
{"name":"testBook","price":0,"author":{}}
可以发现,没有给嵌套结构体赋值时,会打印该嵌套结构体的空结构体。这是因为该空结构体不属于omitempty能识别的空值(false、0、""、nil指针、nil接口、长度为0的数组、切片、映射)。但若期望该嵌套结构体的空结构体也不会输出,可以通过指针实现。
type Book struct{
Name string `json:"name"`
Price float32 `json:"price"`
Desc string `json:"desc,omitempty"`
Author *Author `json:"author,omitempty"`
}
type Author struct {
Gender int `json:"gender"`
Age int `json:"age"`
}
func main() {
var book Book
book.Name = "testBook"
bookByte,_:=json.Marshal(book)
fmt.Printf("%s\n", string(bookByte))
}
输出:
{"name":"testBook","price":0}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持asp之家。如有错误或未考虑完全的地方,望不吝赐教。
来源:https://blog.csdn.net/qq_30505673/article/details/87804194
标签:golang,json,omitempty
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解python列表(list)的使用技巧及高级操作
2021-05-12 15:26:47
2行Python代码实现给pdf文件添加水印
2022-04-03 00:29:26
![](https://img.aspxhome.com/file/2023/1/105031_0s.png)
Vue计时器的用法详解
2024-04-28 09:28:51
![](https://img.aspxhome.com/file/2023/0/132950_0s.jpg)
详解Python实现URL监测与即时推送
2023-12-29 08:18:35
![](https://img.aspxhome.com/file/2023/6/134496_0s.png)
浅谈keras使用预训练模型vgg16分类,损失和准确度不变
2021-05-25 18:23:40
![](https://img.aspxhome.com/file/2023/8/118288_0s.jpg)
配置 SQLServer2005 以允许远程连接
2024-01-18 19:11:01
MySQL 触发器的使用和理解
2024-01-17 07:09:10
![](https://img.aspxhome.com/file/2023/9/65299_0s.png)
Numpy数组转置的两种实现方法
2023-01-22 16:36:54
php解析xml提示Invalid byte 1 of 1-byte UTF-8 sequence错误的处理方法
2023-09-09 18:55:22
django-rest-swagger对API接口注释的方法
2022-04-12 00:06:02
![](https://img.aspxhome.com/file/2023/6/100796_0s.jpg)
ASP 调用带参数输出的COM接口
2011-03-17 10:59:00
Python的numpy库中将矩阵转换为列表等函数的方法
2021-06-19 12:18:21
python将txt文档每行内容循环插入数据库的方法
2024-01-25 21:37:29
用CSS3和HTML5五步打造便签效果
2012-04-25 20:47:51
MySQL5.7.10 安装文档教程详解
2024-01-19 00:37:58
Pycharm安装第三方库时Non-zero exit code错误解决办法
2023-03-15 12:15:01
![](https://img.aspxhome.com/file/2023/2/121732_0s.png)
SQL Server 2005数据库中表的递归查询
2009-01-08 16:08:00
Python实现贪吃蛇小游戏(单人模式)
2023-09-26 23:14:42
python接口自动化测试数据和代码分离解析
2022-12-29 06:00:11
![](https://img.aspxhome.com/file/2023/9/97669_0s.png)
MySQL数据库十大优化技巧
2024-01-25 22:51:28
![](https://img.aspxhome.com/file/2023/9/101539_0s.jpg)