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

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

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
}
}
],

"sentry_server": {
    "host": "127.0.0.1",
    "port": 80
},

"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
}

//bytes, err = StripComments(bytes) //去掉注释
//if err != nil {
//	log.Info("Failed to strip comments from json: %s\n", err)
//	return
//}

//xxx := make(map[string]interface{})
fmt.Println(string(bytes))
err = json.Unmarshal(bytes, &config)
if err != nil {
    fmt.Println("Unmarshal: ", err.Error())
    return
}

fmt.Println(config)

return

}

英文:

json: cannot unmarshal array into Go value of type

config 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
                    }
            }
    ],

    "sentry_server":{
        "host":"127.0.0.1",
        "port":80
    },

    "deadtime":"110000"
}

and my golang code like this:

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 parse code :

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

	//bytes, err = StripComments(bytes) //去掉注释
	//if err != nil {
	//	log.Info("Failed to strip comments from json: %s\n", err)
	//	return
	//}

	//xxx := make(map[string]interface{})
	fmt.Println(string(bytes))
	err = json.Unmarshal(bytes, &config)
	if err != nil {
		fmt.Println("Unmarshal: ", err.Error())
		return
	}

	fmt.Println(config)

	return
}

答案1

得分: 3

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

type ConfigServer struct {
    ServerInfo []ServerInfoStrap `json:"monitor_servers_info"`
    ConnServer SentryServer      `json:"sentry_server"`
    DeadTime   string            `json:"deadtime"`
}

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

英文:

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

type ConfigServer struct {
	ServerInfo []ServerInfoStrap `json:"monitor_servers_info"`
	ConnServer SentryServer      `json:"sentry_server"`
	DeadTime   string            `json:"deadtime"`
}

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:

确定