将[] uint8 / []字节转换为哈希表GoLang

时间:2015-03-23 05:30:12

标签: json go

我的服务器发送JSON响应,如下所示

{"SortAs": "SGML","GlossTerm": "Standard Generalized Markup Language","Acronym": "SGML","Abbrev": "ISO 8879:1986"}

但是My Go程序收到类型为[] uint8。从服务器发送的JSON大小也不是一定的大小,它会有所不同。如何将其转换为包含键/值对的哈希表?

1 个答案:

答案 0 :(得分:3)

请注意,byteuint8的别名。说完你想要的是一个简单的json unmarshal

js := []byte(`{"SortAs": "SGML","GlossTerm": "Standard Generalized Markup Language", "Acronym": "SGML","Abbrev": "ISO 8879:1986"}`)

m := map[string]interface{}{}
if err := json.Unmarshal(js, &m); err != nil {
    panic(err)
}
fmt.Printf("%q",m)

输出(包裹):

map["SortAs":"SGML" "GlossTerm":"Standard Generalized Markup Language"
    "Acronym":"SGML" "Abbrev":"ISO 8879:1986"]

Go Playground上尝试。