golang:Unmarshal:json:无法将数组解析为类型为main.MonitorServerInfo的Go值 [英] golang : Unmarshal: json: cannot unmarshal array into Go value of type main.MonitorServerInfo
本文介绍了golang:Unmarshal:json:无法将数组解析为类型为main.MonitorServerInfo的Go值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
json:无法将数组解析为Go类型的值
config json:
< code $ {
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 {
主机字符串`json:host`
端口int64`json:port`
魔术字符串`json:magic`
Params string`json :params`
Interval int64`json:interval`
}
类型ServerInfoStrap结构{
ConnInfo ServerInfo`json:server_info`
类型MonitorServerInfo结构{
服务器[] ServerInfoStrap
}
类型SentryServer结构{
主机字符串`json:主机
Port int64`json:port`
}
类型ConfigServer结构{
ServerInfo MonitorServerInfo`json:monitor_servers_info`
ConnServer SentryServer`json:sentry_server`
DeadTime字符串`json:deadtime`
}
json解析代码:
func readFile(文件名字符串)(config ConfigServer,err error){
字节,err:= ioutil.ReadFile(文件名)
if err!= nil {
fmt.Println(ReadFile:,err.Error())
return
}
//字节,err = StripComments(字节)//去掉注释
//如果err!= nil {
// log.Info(Failed从json中删除注释:%s \ n,err)
// return
//}
$ b // b:// 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
}
解决方案您的
MonitorServerInfo
类型是问题的原因。
类型ConfigServer结构{
ServerInfo [] ServerInfoStrap`json:monitor_servers_info '
ConnServer SentryServer`json:sentry_server`
DeadTime字符串`json:deadtime`
}
游乐场: https://play.golang.org/p / Prt1j7ePCZ 。
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 }
解决方案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.
这篇关于golang:Unmarshal:json:无法将数组解析为类型为main.MonitorServerInfo的Go值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文