Go:JSON值未解析?

huangapple go评论97阅读模式
英文:

Go: JSON value not parsed?

问题

我有一个非常简单的测试:http://play.golang.org/p/wY4sN9AUky。配置从JSON解析,第一个字符串值解析正常,但第二个解析为空字符串,但实际上不是。

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)  // <- 空字符串。为什么?
    log.Printf("address %v", config.Address)
}
英文:

I have very simple test: http://play.golang.org/p/wY4sN9AUky. Config parsed from JSON, first string value parsed OK, but second parsed to empty string, but it is not.

type Config struct {
	Address      string &quot;address&quot;
	Debug        bool   &quot;debug&quot;
	DbUrl        string &quot;dburl&quot;
	GoogleApiKey string &quot;google_api_key&quot;
}

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

func main() {
	var config Config
	config.read(`{
  &quot;address&quot;: &quot;10.0.0.2:8080&quot;,
  &quot;debug&quot;: true,
  &quot;dburl&quot;: &quot;localhost&quot;,
  &quot;google_api_key&quot;: &quot;the-key&quot;
}`)
	log.Printf(&quot;api key %s&quot;, config.GoogleApiKey)  // &lt;- empty string. why?
	log.Printf(&quot;address %v&quot;, config.Address)
}

答案1

得分: 4

你在结构体中错误地指定了JSON名称。

GoogleApiKey string "google_api_key"

应该是

GoogleApiKey string `json:"google_api_key"`

JSON包会在文本中查找json标头。反引号用于界定原始字符串,允许我们在google_api_key周围包含引号。

http://play.golang.org/p/KNxYhzGLAp

package main

import (
  "log"
  "encoding/json"
)

type Config struct {
  Address string `json:"address"`
  Debug bool `json:"debug"`
  DbUrl string `json:"dburl"`
  GoogleApiKey string `json:"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)
  log.Printf("address %v", config.Address)
}
英文:

You're specifying your JSON names incorrectly in the struct.

GoogleApiKey string &quot;google_api_key&quot;

should be

GoogleApiKey string `json:&quot;google_api_key&quot;`

The JSON package looks for the json header in the text. The backtick delimits a raw string which allows us to include the quotes around google_api_key.

http://play.golang.org/p/KNxYhzGLAp

package main

import (
  &quot;log&quot;
  &quot;encoding/json&quot;
)

type Config struct {
  Address string `json:&quot;address&quot;`
  Debug bool `json:&quot;debug&quot;`
  DbUrl string `json:&quot;dburl&quot;`
  GoogleApiKey string `json:&quot;google_api_key&quot;`
}

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

func main() {
  var config Config
  config.read(`{
  &quot;address&quot;: &quot;10.0.0.2:8080&quot;,
  &quot;debug&quot;: true,
  &quot;dburl&quot;: &quot;localhost&quot;,
  &quot;google_api_key&quot;: &quot;the-key&quot;
}`)
  log.Printf(&quot;api key %s&quot;, config.GoogleApiKey)
  log.Printf(&quot;address %v&quot;, config.Address)
}

huangapple
  • 本文由 发表于 2013年2月18日 12:59:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/14929757.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定