2013-02-05 35 views
2

的JSON字符串的问题是这样的:如何解析Go中的这个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 config 

import(
    "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) 
+0

你看过类型解码器的例子吗http://golang.org/pkg/encoding/json/ –

+0

我看看这篇文章,但我认为我的例子中的diffucutity是我想用map来解析json而不是对象。 – jianfeng

回答

3

要建立在@ peterSO的答案,如果你是为一些花哨的encoding/json封装具有Decoder类型,它允许您JSON直接从解码io.Reader,这是os.File满足的接口。

这将允许您使用os包,而不是io/ioutil,它可以为您节省进口(看到,因为ioutil已经进口os。)

package main 

import (
    "fmt" 
    "encoding/json" 
    "os" 
) 

func main() { 
    pathToFile := "jsondata.txt" 

    file, err := os.OpenFile(pathToFile, os.O_RDONLY, 0644) 
    if err != nil { 
     fmt.Println(err) 
     os.Exit(1) 
    } 

    configs := make(map[string]map[string][]Service, 0) 
    err = json.NewDecoder(file).Decode(&configs) 
    if err != nil { 
     fmt.Println(err) 
     os.Exit(1) 
    } 
} 

这样,你就可以从直接解码JSON文件或数据流。如果你正在做一些简单的事情,并且想要避免这种事情,但是仍然需要注意,这可能是不必要的。

祝你好运!

+0

我认为你的代码更好,非常感谢。 – jianfeng

6

传递的configs地址json.Unmarshal。例如,

configs := make(map[string]map[string][]Service, 0) 
err = json.Unmarshal(content, &configs) 
if err != nil { 
    return nil, err 
} 
fmt.Println(configs) 

输出:

map[production:map[connector:[{connector-server-1 127.0.0.1 4050 0 3050 } {connector-server-2 127.0.0.1 4051 0 3051 } {connector-server-3 127.0.0.1 4052 0 3052 }] gate:[{gate-server-1 127.0.0.1 0 0 3014 }] chat:[{chat-server-1 127.0.0.1 6050 0 0 } {chat-server-2 127.0.0.1 6051 0 0 } {chat-server-3 127.0.0.1 6052 0 0 }]] development:map[chat:[{chat-server-1 127.0.0.1 6050 0 0 } {chat-server-2 127.0.0.1 6051 0 0 } {chat-server-3 127.0.0.1 6052 0 0 }] gate:[{gate-server-1 127.0.0.1 0 0 3014 }] connector:[{connector-server-1 127.0.0.1 4050 0 3050 } {connector-server-2 127.0.0.1 4051 0 3051 } {connector-server-3 127.0.0.1 4052 0 3052 }]]] 
&{[] { 0 0 0 } {{0 0} 0 0 0 0}}