忽略struct中的对象是零,而不是一个空数组
当对象为零而不是空数阵列时,是否可以使用省略时才使用OmitEmpty?
我希望JSON MARSHALLER在对象为零时不显示该值,而是要显示对象:[]
当值为空列表时。
objects: nil
{
...
}
objects: make([]*Object, 0)
{
...
"objects": []
}
Is it possible to only use omitempty when an object is nil and not when it's an empty array?
I would like for the JSON marshaller to not display the value when an object is nil, but show object: []
when the value is an empty list.
objects: nil
{
...
}
objects: make([]*Object, 0)
{
...
"objects": []
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
https://go.dev/play/play/p/zpi39iou-p5
https://go.dev/play/p/ZPI39ioU-p5
您将需要为您的结构创建自定义的JSON元帅/Unmarshal功能。 something like:
Output:
Run above example: https://goplay.tools/snippet/J_iKIJ9ZMhT
You will need to create a custom json Marshal/Unmarshal functions for your struct. something like:
Output:
Run above example: https://goplay.tools/snippet/J_iKIJ9ZMhT