如何确定哪种结构适合容纳 json 文件

我有一个json文件,如下所示:


{

   "Key1": "value1",

   "Key2": [

      "value2",

      "value3",

   ],

}

我试图使用下面的结构来反序列化json,但是,在反序列化之后,只有key2有值,key1是空的。


问:反序列化此 json 的正确结构是什么?


data := map[string][]string{}

_ = json.Unmarshal([]byte(file), &data)


侃侃无极
浏览 151回答 1
1回答

慕无忌1623718

用structtype 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)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go