Go:未解析JSON值?

Go:未解析JSON值?,json,go,Json,Go,我有一个非常简单的测试:。从JSON解析配置,第一个字符串值解析为OK,但第二个解析为空字符串,但不是 type Config struct { Address string "address" Debug bool "debug" DbUrl string "dburl" GoogleApiKey string "google_api_key" } func (cfg *Config) read(json_code s

我有一个非常简单的测试:。从JSON解析配置,第一个字符串值解析为OK,但第二个解析为空字符串,但不是

type Config struct {
    Address      string "address"
    Debug        bool   "debug"
    DbUrl        string "dburl"
    GoogleApiKey string "google_api_key"
}

func (cfg *Config) read(json_code string) {
    if e := json.Unmarshal([]byte(json_code), cfg); e != nil {
        log.Printf("ERROR JSON decode: %v", e)
    }
}

func main() {
    var config Config
    config.read(`{
  "address": "10.0.0.2:8080",
  "debug": true,
  "dburl": "localhost",
  "google_api_key": "the-key"
}`)
    log.Printf("api key %s", config.GoogleApiKey)  // <- empty string. why?
    log.Printf("address %v", config.Address)
}
类型配置结构{
地址字符串“地址”
调试布尔“调试”
DbUrl字符串“DbUrl”
GoogleAPI密钥字符串“google\u api\u密钥”
}
func(cfg*Config)读取(json_代码字符串){
如果e:=json.Unmarshal([]字节(json\u代码),cfg);e!=零{
Printf(“错误JSON解码:%v”,e)
}
}
func main(){
变量配置
config.read(`{
“地址”:“10.0.0.2:8080”,
“调试”:正确,
“dburl”:“localhost”,
“谷歌api密钥”:“密钥”
}`)

log.Printf(“api键%s”,config.GoogleApiKey)/您在结构中错误地指定了JSON名称

GoogleApiKey string "google_api_key"
应该是

GoogleApiKey string `json:"google_api_key"`
JSON包在文本中查找
JSON
标题。反勾分隔了一个原始字符串,允许我们在google\u api\u键周围包含引号


您在结构中错误地指定了JSON名称

GoogleApiKey string "google_api_key"
应该是

GoogleApiKey string `json:"google_api_key"`
JSON包在文本中查找
JSON
标题。反勾分隔了一个原始字符串,允许我们在google\u api\u键周围包含引号


只对
json真正需要:“google\u api\u key”
。其他映射到小写版本。只对
json真正需要:“google\u api\u key”
。其他映射到小写版本。