在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
序列化与反序列化 package main import ( "encoding/json" "fmt" ) //JSON的序列化 //将结构体构成数据,并转JSON //将map[string]interface{}构成数据,并转JSON //使用map切片构成数据,并转JSON //json.cn //将结构体构成数据,并转JSON //使用Man 结构体构成数据,并转为json type Man struct{ Name string Age int Hobby []string Sex bool } func maina1(){ person := Man{"liyi",20,[]string{"唱歌","跳舞"},true} bytes,err := json.Marshal(person) if err != nil{ fmt.Println("序列化失败,err=",err) return }else{ fmt.Println("序列化成功,err=",err) fmt.Println(string(bytes)) } } //将map[string]interface{}构成数据,并转JSON //这里interface{}代表任意数据类型 func maina2(){ dataMap := make(map[string]interface{}) dataMap["name"] ="liyi" dataMap["age"] = 20 dataMap["hobby"] = []string{"唱歌","跳舞"} dataMap["sex"] = true bytes,err := json.Marshal(dataMap) if err != nil{ fmt.Println("序列化失败,err=",err) return }else{ fmt.Println("序列化成功,err=",err) fmt.Println(string(bytes)) } } //使用map切片构成数据,并转JSON func maina3(){ dataMap1 := make(map[string]interface{}) dataMap1["name"] ="liyi" dataMap1["age"] = 20 dataMap1["hobby"] = []string{"唱歌","跳舞"} dataMap1["sex"] = true dataMap2 := make(map[string]interface{}) dataMap2["name"] ="linging" dataMap1["age"] = 21 dataMap1["hobby"] = []string{"打炮"} dataMap1["sex"] = true dataMap3 := make(map[string]interface{}) dataMap3["name"] ="yiyi" dataMap3["age"] = 18 dataMap3["hobby"] = []string{"学习"} dataMap3["sex"] = true dataSlice := make([]map[string]interface{}, 0) dataSlice = append(dataSlice,dataMap1,dataMap2,dataMap3) bytes,err := json.Marshal(dataSlice) if err != nil{ fmt.Println("序列化失败,err=",err) return }else{ fmt.Println("序列化成功") fmt.Println(string(bytes)) } } //JSON的反序列化 //将json数据转为map //将json数据转为结构体 //将json数据转换为map切片 //将json数据转为结构体切片 //将json数据转为map func maina4(){ //json数据 jsonStr := `{"Name":"liyi","Age":18,"Hobby":["抽烟","喝酒"],"sex":true}` //将json数据转为字节数据 jsonbytes := []byte(jsonStr) dataMap := make(map[string]interface{}) err := json.Unmarshal(jsonbytes,&dataMap) if err != nil { fmt.Println("反序列化失败,err=",err) } fmt.Println(dataMap) } //将json数据转为结构体 func maina5(){ type Man struct{ Name string Age int Hobby []string Sex bool } //json数据 jsonStr := `{"Name":"liyi","Age":18,"Hobby":["抽烟","喝酒"],"sex":true}` //将json数据转为字节数据 jsonbytes := []byte(jsonStr) //创建一个结构体 Man1 := new(Man) err := json.Unmarshal(jsonbytes,&Man1) if err != nil { fmt.Println("反序列化失败,err=",err) } fmt.Println(*Man1) } //将json数据转换为map切片 func maina6(){ jsonStr := `[{"age":21,"hobby":["打炮"],"name":"liyi","sex":true},{"name":"linging"},{"age":18,"hobby":["学习"],"name":"yiyi","sex":true}]` jsonBytes := []byte(jsonStr) dataSlice := make([]map[string]interface{},0) err := json.Unmarshal(jsonBytes,&dataSlice) if err != nil { fmt.Println("反序列化失败,err=",err) } fmt.Println(dataSlice) } //将json数据转为结构体切片 func maina7() { type Man struct { Name string Age int Hobby []string Sex bool } jsonStr := `[{"age":21,"hobby":["打炮"],"name":"liyi","sex":true},{"name":"linging"},{"age":18,"hobby":["学习"],"name":"yiyi","sex":true}]` jsonBytes := []byte(jsonStr) Mans := make([]Man, 0) err := json.Unmarshal(jsonBytes, &Mans) if err != nil { fmt.Println("反序列化失败,err=", err) } fmt.Println(Mans) }
Json编码与解码 //编码:go数据--》json文件 //解码:json文件--》go数据 //使用json包编码 //注意:解码json数据,需要对应的数据类型的数据来接收!!!<br> //将map类型数据编码为json文件 func mainb0() { //go数据:map类型 dataMap1 := make(map[string]interface{}) dataMap1["name"] = "liyi" dataMap1["age"] = 20 dataMap1["hobby"] = []string{"唱歌", "跳舞"} dataMap1["sex"] = true //打开文件 dstFile, _ := os.OpenFile("C:/test/test1.json", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666) defer dstFile.Close() //创建编码器 encoder := json.NewEncoder(dstFile) //编码 err := encoder.Encode(dataMap1) if err != nil { fmt.Println("编码失败,err=", err) return } fmt.Println("编码成功") } //将结构体切片编码转为json文件 func mainb1(){ type Man struct { Name string Age int Hobby []string Sex bool } person1 := Man{"liyi",20,[]string{"唱歌","跳舞"},true} person2 := Man{"yiyi",20,[]string{"唱歌","跳舞"},true} person3 := Man{"meng",20,[]string{"唱歌","跳舞"},true} people := make([]Man,0) people = append(people,person1,person2,person3) dstFile,_ := os.OpenFile("C:/test/test1.json",os.O_CREATE | os.O_WRONLY | os.O_APPEND,0666) defer dstFile.Close() encoder := json.NewEncoder(dstFile) err := encoder.Encode(people) if err != nil { fmt.Println("编码失败,err=", err) return } fmt.Println("编码成功") } //解码json文件为map func mainc1(){ //打开json文件 srcFile,_ := os.Open("C:/test/test1.json") defer srcFile.Close() //创建接收数据的map类型数据 datamap := make(map[string]interface{}) //创建解码器 decoder := json.NewDecoder(srcFile) //解码 err := decoder.Decode(&datamap) if err != nil{ fmt.Println("解码失败,err=",err) return } fmt.Println("解码成功",datamap) } //解码json文件为结构体 func mainc2(){ type Man struct { Name string Age int Hobby []string Sex bool } //打开json文件 srcFile,_ := os.Open("C:/test/test1.json") defer srcFile.Close() //创建接收数据的结构体数据 datastruct := make([]Man,0) decoder := json.NewDecoder(srcFile) //解码 err := decoder.Decode(&datastruct) if err != nil{ fmt.Println("解码失败,err=",err) return } fmt.Println("解码成功",datastruct) }
|
请发表评论