go json配置

问题1:被序列化的结构体首字母必须大写

type Student struct {
sex string
age int
}

如果被序列化的结构体首字母不大写,那么序列化结果是空。

告警内容

struct type 'test/json_config.Student' doesn't have any exported fields, nor custom marshaling

问题2:不指定json配置后序列化结果key首字母大写

type Student struct {
Sex string
Age int
}

对应

{"Sex":"s1","Age":20}

推荐使用json配置,`json:"xxx,omitempty,-"`表示序列化时key写成xxx,反序列化时根据xxx寻找值;omitempty表示反序列化时忽略零值(例如int零值是0);-表示该值不序列化。这里只是示例,没有实际意义。

type Student struct {
Sex string `json:"sex"`
Age int `json:"age"`
}

对应

{"sex":"s1","age":20}

 

posted on   王景迁  阅读(5)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示