从过去 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 jsonFile
jsonFile, 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 ans
json_data := result["Info"]["Date"] // returns error - type interface {} does not support indexing
任何帮助/领导都将受到高度赞赏。非常感谢。
json去编组解组
ITMISS
猛跑小猪
相关分类