typeStudent struct { Name string Sex string Age int //position Position PositionPosition }
改成大写后再观察结果,可以正常序列化。
序列化到json后改成小写
对于json串,很多人喜欢全小写,对于大写开头的key感觉很刺眼,我们继续改进:
1 2 3 4 5 6 7 8 9 10 11 12
type Position struct { X int `json:"x"` Y int `json:"y"` Z int `json:"z"` }
type Student struct { Name string `json:"name"` Sex string `json:"sex"` Age int `json:"age"` Posi Position `json:"position"` }
再次运行程序,结果是我们期望的,打印如下:
1 2 3 4
Init:srcSlice is : [{zhangsan male 20 {102030}} {lisi female 18 {151020}}] After serialize, data : [{"name":"zhangsan","sex":"male","age":20,"position":{"x":10,"y":20,"z":30}},{"name":"lisi","sex":"female","age":18,"position":{"x":15,"y":10,"z":20}}] Deserialize:dstSlice is : [{zhangsan male 20 {102030}} {lisi female 18 {151020}}]