我有一個json文件,如下所示:{ "Key1": "value1", "Key2": [ "value2", "value3", ],}我試圖使用下面的結構來反序列化json,但是,在反序列化之后,只有key2有值,key1是空的。問:反序列化此 json 的正確結構是什么?data := map[string][]string{}_ = json.Unmarshal([]byte(file), &data)
1 回答

慕無忌1623718
TA貢獻1744條經驗 獲得超4個贊
用struct
type Test struct {
Key1 string
Key2 []string
}
func main() {
testJson := `{"Key1": "value1","Key2": ["value2","value3"]}`
var test Test
json.Unmarshal([]byte(testJson), &test)
fmt.Printf("%s, %s", test.Key1 , test.Key2 )
}
使用map
我們創建一個指向空接口的字符串映射:
var result map[string]interface{}
testJson := `{"Key1": "value1","Key2": ["value2","value3"]}`
var result map[string]interface{}
json.Unmarshal([]byte(testJson ), &result)
- 1 回答
- 0 關注
- 99 瀏覽
添加回答
舉報
0/150
提交
取消