``` type jsonset struct { Sqluser string `json:"sqluser"` Sqlpassword string `json:"sqlpassword"` } func main() { dile, err := os.Open("./seeting.json") if err != nil { fmt.Printf("文件打开失败 [Err:%s]\n", err.Error()) return } defer dile.Close() bytejson, _ := ioutil.ReadAll(dile) jsonsets := &jsonset{} json.Unmarshal(bytejson, jsonsets) fmt.Println(jsonsets) } ``` 解析: 首先定义一个接收内容的解析器架构体。 os.open打开文件句柄。 ioutil.ReadAll读取文件内容。 json.Unmarshal将内容赋值给结构体。 最后打印内容即可 Loading... ``` type jsonset struct { Sqluser string `json:"sqluser"` Sqlpassword string `json:"sqlpassword"` } func main() { dile, err := os.Open("./seeting.json") if err != nil { fmt.Printf("文件打开失败 [Err:%s]\n", err.Error()) return } defer dile.Close() bytejson, _ := ioutil.ReadAll(dile) jsonsets := &jsonset{} json.Unmarshal(bytejson, jsonsets) fmt.Println(jsonsets) } ``` 解析: 首先定义一个接收内容的解析器架构体。 os.open打开文件句柄。 ioutil.ReadAll读取文件内容。 json.Unmarshal将内容赋值给结构体。 最后打印内容即可 最后修改:2021 年 06 月 27 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 如果觉得我的文章对你有用,请随意赞赏