從過(guò)去 2 天開始,我不知怎么地就被 JSON 和 Go 困住了。我的目標(biāo)非常簡(jiǎn)單,一個(gè) Go 程序可以讀取 JSON 文件,正確輸出它并向該 JSON 添加一些項(xiàng)目,然后將其重寫回磁盤。保存的 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"] } }}我已經(jīng)在 Go 中成功解析了 JSON,但是當(dāng)我嘗試獲取 JSON["Info"]["Date"] 時(shí),它會(huì)拋出接口錯(cuò)誤。我無(wú)法創(chuàng)建特定的結(jié)構(gòu),因?yàn)槊慨?dāng)調(diào)用代碼/API 時(shí),所有項(xiàng)目都會(huì)動(dòng)態(tài)更改。我用來(lái)解析數(shù)據(jù)的代碼// 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任何幫助/領(lǐng)導(dǎo)都將受到高度贊賞。非常感謝。json去編組解組
2 回答

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