2016-11-16 7 views
-1

JSONの囲碁値に変換することはできませんアンマーシャリングのアレイ:このようなgolang:非整列化:JSON:タイプmain.MonitorServerInfo

{ 
    "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コード:タイプ

設定JSONの囲碁値に変換することはできませんアンマーシャリング配列:

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 
} 

答えて

関連する問題