Go omitempty bool字段- 不显示false类型

huangapple go评论66阅读模式
英文:

Go omitempty bool field- not showing false type

问题

我有一个结构体:

type Settings struct {
    Status           bool `json:"status,omitempty"`
    AvailableStatus  bool `json:"available_status,omitempty"`
}

我在我的 GO API 中使用这个结构体来保存和显示数据。

例如,如果我的保存数据的 JSON 主体如下所示:

{
    "settings": {
        "status": true,
        "available_status": false
    }
}

保存后,我使用 Get-API 获取数据,结果如下:

{
    "settings": {
        "status": true
    }
}

只有 true 的数据被显示出来,我想要同时显示两个数据,并且还需要设置 omitempty(在保存时忽略空值,然后使用 JSON 进行验证)。

我该如何做到这一点?

对于保存数据,不是所有字段都是必需的。在我的 API 主体中,我可以给出以下 JSON:

{
    "settings": {
        "status": true,
        "available_status": false
    }
}

或者

{
    "settings": {
        "status": true
    }
}

我也想要这样做。我为每个模型创建了 JSON,并在 JSON 中进行验证。如果我没有添加 omitempty 字段,它将显示错误,要求 available_status 是必需的。

或者还有其他方法可以将 available_status 设置为必需字段吗?

英文:

I have a struct

type Settings struct {
	Status             bool `json:"status,omitempty"`
	AvailableStatus        bool `json:"available_status,omitempty"`
}

I am using this struct for saving the data and displaying the data in my GOAPis

for eg if for my save data my json body is like this

{"settings":{"status":true,
"available_status":false}}

after save I fetch data data using Get-API I am getting like this

"settings": {
        "status": true
    }

only true data is displaying I need to display both data and need to set omitempty also(omit empty for saving, after that json created and using json I am checking validation)

How can I do this?

for saving the data, all fields are not required.
I might be able to give json like below in my apis body.

{"settings":{"status":true,
"available_status":false}}

or

{"settings":{"status":true}}

I want to do this also. I created json for each model and validation is checking in json .. If I not added omitempty field it will show error available_status is required.

Or any other method for setting available_status as required filed..

答案1

得分: 1

"omitempty"只在值等于所选类型的零值时省略。如果你想从结构体创建自定义的JSON,你可以将结构体转换为map,然后使用json.Marshal或其他类似的库,比如https://github.com/tidwall/sjson。

英文:

"omitempty" is omitting only when value is equal to zerovalue of chosen type. If you want to create custom json from struct you can make map from struct and use json.Marshal or other lib like https://github.com/tidwall/sjson

答案2

得分: 0

使用指针

type Settings struct {
    Status             *bool `json:"status,omitempty"`
    AvailableStatus    *bool `json:"available_status,omitempty"`
}

使用指针可以在需要时将字段设置为零值或空值。在上面的代码中,StatusAvailableStatus字段都是指向bool类型的指针。通过使用指针,可以将它们设置为nil,表示没有提供值。此外,omitempty标记表示如果字段的值为零值或空值,则在JSON序列化时忽略该字段。

英文:

Use pointers

type Settings struct {
    Status             *bool `json:"status,omitempty"`
    AvailableStatus    *bool `json:"available_status,omitempty"`
}


</details>



huangapple
  • 本文由 发表于 2022年5月12日 03:06:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/72206365.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定