go 读取json文件转换go数据

package main

import (
	"encoding/json"
	"fmt"
	"os"
)

type beautyI struct {
	Name   string
	Age    int
	Weight float32
	Bwh    []int
}

var zzmx beautyI

func main() {
	//打开文件
	file, err := os.Open("E:\\workspace\\Goland\\json正反序列化\\zzm.json")
	if err != nil {
		fmt.Println("打开文件失败,错误:",err)
		return
	}
	//声明文件读取在函数结束时关闭
	defer file.Close()
	decoder := json.NewDecoder(file)
	err = decoder.Decode(&zzmx)
	if err==nil {
		fmt.Printf("json数据读取成功:%#v",zzmx)
	}else{
		fmt.Println("json数据读取失败,错误:",err)
	}

}

输出:

json数据读取成功:main.beautyI{Name:"佐々木あき", Age:40, Weight:48.8, Bwh:[]int{82, 58, 86}}
 

猜你喜欢

转载自blog.csdn.net/ckx178/article/details/88050042
go