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解析出来了,为什么alterId0,为什么不是2?

最佳答案

alterId 开头改为大写:AlterId, 小写是不导出的。

3年前 评论
讨论数量: 2

alterId 开头改为大写:AlterId, 小写是不导出的。

3年前 评论

就是,struct 属性一般都是大写,否则解析或者其他地方调用是不行的

3年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!