golang: Unmarshal: json: 无法将数组解组为类型为main.MonitorServerInfo的Go值

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

golang : Unmarshal: json: cannot unmarshal array into Go value of type main.MonitorServerInfo

问题

json:无法将数组解组为Go类型的值

json配置:

{
"monitor_servers_info": [
{
"server_info": {
"host": "127.0.0.1",
"port": 28081,
"magic": "magic0",
"params": "all",
"interval": 10000
}
},
{
"server_info": {
"host": "127.0.0.1",
"port": 28080,
"magic": "magic1",
"params": "all",
"interval": 10000
}
}
],

  1. "sentry_server": {
  2. "host": "127.0.0.1",
  3. "port": 80
  4. },
  5. "deadtime": "110000"

}

我的Golang代码如下:

type ServerInfo struct {
Host string json:"host"
Port int64 json:"port"
Magic string json:"magic"
Params string json:"params"
Interval int64 json:"interval"
}

type ServerInfoStrap struct {
ConnInfo ServerInfo json:"server_info"
}

type MonitorServerInfo struct {
Servers []ServerInfoStrap
}

type SentryServer struct {
Host string json:"host"
Port int64 json:"port"
}

type ConfigServer struct {
ServerInfo MonitorServerInfo json:"monitor_servers_info"
ConnServer SentryServer json:"sentry_server"
DeadTime string json:"deadtime"
}

JSON解析代码:

func readFile(filename string) (config ConfigServer, err error) {
bytes, err := ioutil.ReadFile(filename)
if err != nil {
fmt.Println("ReadFile: ", err.Error())
return
}

  1. //bytes, err = StripComments(bytes) //去掉注释
  2. //if err != nil {
  3. // log.Info("Failed to strip comments from json: %s\n", err)
  4. // return
  5. //}
  6. //xxx := make(map[string]interface{})
  7. fmt.Println(string(bytes))
  8. err = json.Unmarshal(bytes, &config)
  9. if err != nil {
  10. fmt.Println("Unmarshal: ", err.Error())
  11. return
  12. }
  13. fmt.Println(config)
  14. return

}

英文:

json: cannot unmarshal array into Go value of type

config json:

  1. {
  2. "monitor_servers_info":[
  3. {
  4. "server_info":{
  5. "host":"127.0.0.1",
  6. "port":28081,
  7. "magic":"magic0",
  8. "params":"all",
  9. "interval":10000
  10. }
  11. },
  12. {
  13. "server_info":{
  14. "host":"127.0.0.1",
  15. "port":28080,
  16. "magic":"magic1",
  17. "params":"all",
  18. "interval":10000
  19. }
  20. }
  21. ],
  22. "sentry_server":{
  23. "host":"127.0.0.1",
  24. "port":80
  25. },
  26. "deadtime":"110000"
  27. }

and my golang code like this:

  1. type ServerInfo struct {
  2. Host string `json:"host"`
  3. Port int64 `json:"port"`
  4. Magic string `json:"magic"`
  5. Params string `json:"params"`
  6. Interval int64 `json:"interval"`
  7. }
  8. type ServerInfoStrap struct {
  9. ConnInfo ServerInfo `json:"server_info"`
  10. }
  11. type MonitorServerInfo struct {
  12. Servers []ServerInfoStrap
  13. }
  14. type SentryServer struct {
  15. Host string `json:"host"`
  16. Port int64 `json:"port"`
  17. }
  18. type ConfigServer struct {
  19. ServerInfo MonitorServerInfo `json:"monitor_servers_info"`
  20. ConnServer SentryServer `json:"sentry_server"`
  21. DeadTime string `json:"deadtime"`
  22. }

json parse code :

  1. func readFile(filename string) (config ConfigServer, err error) {
  2. bytes, err := ioutil.ReadFile(filename)
  3. if err != nil {
  4. fmt.Println("ReadFile: ", err.Error())
  5. return
  6. }
  7. //bytes, err = StripComments(bytes) //去掉注释
  8. //if err != nil {
  9. // log.Info("Failed to strip comments from json: %s\n", err)
  10. // return
  11. //}
  12. //xxx := make(map[string]interface{})
  13. fmt.Println(string(bytes))
  14. err = json.Unmarshal(bytes, &config)
  15. if err != nil {
  16. fmt.Println("Unmarshal: ", err.Error())
  17. return
  18. }
  19. fmt.Println(config)
  20. return
  21. }

答案1

得分: 3

你的MonitorServerInfo类型是问题的原因。去掉它就可以解决问题:

  1. type ConfigServer struct {
  2. ServerInfo []ServerInfoStrap `json:"monitor_servers_info"`
  3. ConnServer SentryServer `json:"sentry_server"`
  4. DeadTime string `json:"deadtime"`
  5. }

Playground链接:https://play.golang.org/p/Prt1j7ePCZ。

英文:

Your MonitorServerInfo type is the cause of the problem. Get rid of it and it works:

  1. type ConfigServer struct {
  2. ServerInfo []ServerInfoStrap `json:"monitor_servers_info"`
  3. ConnServer SentryServer `json:"sentry_server"`
  4. DeadTime string `json:"deadtime"`
  5. }

Playground: https://play.golang.org/p/Prt1j7ePCZ.

huangapple
  • 本文由 发表于 2016年11月16日 15:14:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/40626125.html
匿名

发表评论

匿名网友

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

确定