douyong1850 2016-11-16 07:14
浏览 62
已采纳

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

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
", 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条回答 默认 最新

  • doupixian1436 2016-11-16 08:01
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行