json 序列化大小写总结

1,结构体没有tag,字段原样打印,大小写不变,小写字母开头的字段不会被打印
user := User{
Name: “小花”,
Age: 18,
Height: 162,
Weight: 42,
}
// struct–转–>json string
str_json, err := json.Marshal(user)



str_json:{“Name”:”小花”,”Age”:18,”Height”:162,”Weight”:42}



2,字典字段打印也是原样打印,小写字段也可以被打印
m[“count”] = 1 // int
b, err := json.Marshal(m)
打印结果:str:”{"count":1}”



https://blog.csdn.net/qq_33867131/article/details/106944121

3,unmarshal的时候,结构体字段首字母大写,才能成功,没有tag,json首字母小写的字段会被转成同名字段
type Actress struct {
Name string
Birthday string
BirthPlace string
Opus []string
}



jsonData := []byte({
"name":"迪丽热巴",
"birthday":"1992-06-03",
"birthPlace":"新疆乌鲁木齐市",
"opus":[
"《阿娜尔罕》",
"《逆光之恋》",
"《克拉恋人》"
]
}
)
err := json.Unmarshal(jsonData, &actress)



https://blog.csdn.net/wowenlong/article/details/113571853



Category golang