json.Unmarshal解析问题
结构体定义:
type V2Ray struct {
Name string `json:"ps"`
Server string `json:"add"`
Port int64 `json:"port"`
Passwd string `json:"id"`
alterId int64 `json:"aid"`
Obfs string `json:"net"`
ObfsParam string `json:"path"`
}
待解析的JSON文本:
{
"v": 2,
"ps": "学习助理",
"add": "VOLAKT.China-nCDN.com",
"port": 231,
"id": "1b3e73d1-c1ba-3f3e-ad6b",
"aid": 2,
"net": "ws",
"type": "none",
"host": "",
"path": "/iso",
"tls": ""
}
o := V2Ray{}
json.Unmarshal(b, &o)
打印解析结果:
fmt.Printf("\n%#v\n", o)
实际输出:
V2Ray{Name:"31.学习助理", Server:"SCDN.1031.VOLAKT.China-nCDN.com", Port:231, Passwd:"1b3e73d1-c1ba-3f3e-ad6b", alterId:0, Obfs:"ws", ObfsParam:"/iso"}
疑问:Port
解析出来了,为什么alterId
是0
,为什么不是2
?
alterId
开头改为大写:AlterId
, 小写是不导出的。