亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在Go中解析此JSON字符串?

如何在Go中解析此JSON字符串?

Go
慕絲7291255 2021-05-04 17:29:34
有問題的JSON字符串如下所示:{"development":{    "connector":[         {"id":"connector-server-1", "host":"127.0.0.1", "port":4050, "wsPort":3050},         {"id":"connector-server-2", "host":"127.0.0.1", "port":4051, "wsPort":3051},         {"id":"connector-server-3", "host":"127.0.0.1", "port":4052, "wsPort":3052}     ],    "chat":[         {"id":"chat-server-1", "host":"127.0.0.1", "port":6050},         {"id":"chat-server-2", "host":"127.0.0.1", "port":6051},         {"id":"chat-server-3", "host":"127.0.0.1", "port":6052}    ],    "gate":[     {"id": "gate-server-1", "host": "127.0.0.1", "wsPort": 3014}]},"production":{   "connector":[         {"id":"connector-server-1", "host":"127.0.0.1", "port":4050, "wsPort":3050},         {"id":"connector-server-2", "host":"127.0.0.1", "port":4051, "wsPort":3051},         {"id":"connector-server-3", "host":"127.0.0.1", "port":4052, "wsPort":3052}     ],    "chat":[         {"id":"chat-server-1", "host":"127.0.0.1", "port":6050},         {"id":"chat-server-2", "host":"127.0.0.1", "port":6051},         {"id":"chat-server-3", "host":"127.0.0.1", "port":6052}    ],    "gate":[     {"id": "gate-server-1", "host": "127.0.0.1", "wsPort": 3014}]}}我想用這樣的代碼來解析它:package configimport(    "sync"    "io/ioutil"    "encoding/json"    "errors"    "log")type Service struct {    Id string `json:"id"`    Host string `json:"host"`    Port uint `json:"port"`    QueryPort uint `json:"queryPort"`    WsPort uint `json:"wsPort"`    ServiceType string }type Config struct {    Services []Service    Master Service    Mutex sync.RWMutex}func LoadServers(filepath, env string) (*Config, error) {    // 讀取文件    content, err := ioutil.ReadFile(filepath)    if err != nil {        return nil, err    }    configs := make(map[string]map[string][]Service, 0)    err = json.Unmarshal(content, configs)    if err != nil {        return nil, err    }}我希望我的代碼將此JSON字符串解析為map[string]map[string][]Service。但它顯示錯誤:json: Unmarshal(non-pointer map[string]map[string][]config.Service)
查看完整描述

2 回答

  • 2 回答
  • 0 關注
  • 313 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號