Go 的 json 省略符 omitempty 和 - 兩種方式詳解

一、介紹

官方文檔 https://pkg.go.dev/encoding/json 有介紹

二、omitempty 空值省略

2.1 json 在結構體重的寫法

我們經常看到如下的結構體中,用 tag 標註瞭解析 json 的格式
首先我們初始化一個空結構體 Product

package json

import (
    "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 json

import (
    "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 json

import (
    "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 json

import (
    "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 json

import (
    "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
本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/crM5FcxIYlUA4CxRbbaqEQ