從過去 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任何幫助/領導都將受到高度贊賞。非常感謝。json去編組解組
2 回答

猛跑小豬
TA貢獻1858條經驗 獲得超8個贊
您無法使用 訪問內部屬性result[][]。你需要做如下的事情,
info:= result["Info"]
v := info.(map[string]interface{})
json_data = v["Date"]
- 2 回答
- 0 關注
- 208 瀏覽
添加回答
舉報
0/150
提交
取消