dongpi9494 2014-06-25 13:11
浏览 39
已采纳

强制转换界面{}以json编码进行结构化

I have such code: http://play.golang.org/p/aeEVLrc7q1

type Config struct { 
    Application interface{} `json:"application"`
}

type MysqlConf struct {
    values map[string]string `json:"mysql"`
}

func main() {
    const jsonStr = `
        {
            "application": {
                "mysql": {
                    "user": "root",
                    "password": "",
                    "host": "localhost:3306",
                    "database": "db"
                }   
            }
        }`

    dec := json.NewDecoder(strings.NewReader(jsonStr))
    var c Config 
    c.Application = &MysqlConf{}
    err := dec.Decode(&c)
    if err != nil {
        fmt.Println(err)
    }
}

And I don't know why resulting struct is empty. Do you have any ideas?

  • 写回答

1条回答 默认 最新

  • dongyuji7309 2014-06-25 13:13
    关注

    You did not export values in the MysqlConf structure, so the json package was not able to use it. Use a capital letter in the variable name to do so:

    type MysqlConf struct {
        Values map[string]string `json:"mysql"`
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?