json配置为
{
"items": [
{
"url": "https://xxx.com",
"name": "xxx"
}
]
}
对应解析为
import "encoding/json"
type CfgItem struct {
Url string
Name string
Check bool
}
type Cfg struct {
Items []CfgItem
}
cfg := &Cfg{}
json.Unmarshal([]byte(fileData), &cfg)
fmt.Println(*cfg)
这里有个小坑,注意所有变量名必须大写开头,否则无法反序列化
PS:如果觉得自己设计结构太费脑子,可以用这个工具
https://mholt.github.io/json-to-go/