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 2024-09-26 20:39  王景迁  阅读(1)  评论(0编辑  收藏  举报

导航