从过去 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 关注
- 118 浏览
添加回答
举报
0/150
提交
取消