從過去 2 天開始,我不知何故被 JSON 和 Go 困住了。我的目標很簡單,一個 Go 程序可以讀取 JSON 文件,正確輸出并將一些項目附加到該 JSON 中,然后將其重寫回磁盤。保存的 JSON 文件。{"Category": ["food","music"],"Time(min)": "351","Channel": { "d2d": 10, "mkbhd": 8, "coding Train": 24},"Info": { "Date":{ "date":["vid_id1","vid_id2","vid_id3"], "02/11/2019":["id1","id2","id3"], "03/11/2019":["SonwZ6MF5BE","8mP5xOg7ijs","sc2ysHjSaXU"] }, "Videos":{ "videos": ["Title","Category","Channel","length"], "sc2ysHjSaXU":["Bob Marley - as melhores - so saudade","Music","So Saudade","82"], "SonwZ6MF5BE":["Golang REST API With Mux","Science & Technology","Traversy Media","44"], "8mP5xOg7ijs":["Top 15 Funniest Friends Moments","Entertainment","DjLj11","61"] } }}我已經在 Go 中成功解析了 JSON,但是當我嘗試獲取 JSON["Info"]["Date"] 時,它會引發接口錯誤。我無法制作特定的結構,因為只要調用代碼/API,所有項目都會動態更改。我用來解析數據的代碼// Open our jsonFilejsonFile, err := os.Open("yt.json")if err != nil {fmt.Println(err)}fmt.Println("Successfully Opened yt.json")defer jsonFile.Close()byteValue, _ := ioutil.ReadAll(jsonFile)var result map[string]interface{}json.Unmarshal([]byte(byteValue), &result)json_data := result["Category"] //returns correct ansjson_data := result["Info"]["Date"] // returns error - type interface {} does not support indexing任何幫助/領導都非常感謝。提前非常感謝。
2 回答

慕虎7371278
TA貢獻1802條經驗 獲得超4個贊
您無法使用result[][]. 您需要執行以下操作,
info:= result["Info"]
v := info.(map[string]interface{})
json_data = v["Date"]
- 2 回答
- 0 關注
- 155 瀏覽
添加回答
舉報
0/150
提交
取消