1、env.json文件内容:
{
"option" :[
{"aaa" : 10}
]
}
2、 go文件(调用json的文件):
①、定义结构体:
type Size struct {
Cache []map[string]int 'json:"option"'
}
②、代码实现:
filePrt, err := os.Open("./env.json")
if err != nil{
fmt.Println("file open failed", err.Error())
return
}
defer filePrt.Close()
var info Size
//create json encoder
docoder := json.NewDecoder(filePrt)
err = docoder.Decode(&info)
cacheSize := info.Cache[0]["aaa"]
fmt.Println(cacheSize) //输出结果为10(json文件中定义的aaa的值)