go的json序列化,以及tag使用
点击查看代码
package main
import (
"encoding/json"
"fmt"
)
type User struct {
UserName string `json:"username"` // 有了tag,序列化出来的就是tag中的名字
Sex string `json:"sex"`
Score float32 // 没有设置tag默认是自己的
age int32 // 小写的是私有的, 其他包不可以访问,json序列化时会自动把这个给忽略
点击查看代码
输出:
点击查看代码
bytes str:[123 34 117 115 101 114 110 97 109 101 34 58 34 117 115 101 114 48 49 34 44 34 115 101 120 34 58 34 231 148 183 34 44 34 83 99 111 114 101 34 58 57 57 46 53 125]
json str:{"username":"user01","sex":"男","Score":99.5}