总结
/*
序列化与反序列化
序列化: 将go语言数据转化为JSON --- json.Marshal(goData)
反序列化: 将JSON转化为GO语言数据. --- json.Unmarshal(JSONBytes,goDataPtr)
读写JSON文件
编码: 将GO语言数据写出到JSON文件 ---- encoder := json.NewEncoder(dstFile) encoder.Encode(goData)
解码: 将JSON文件中的数据读取为GO语言数据. ---- encoder := json.NewDecoder(srcFile) encoder.Decode(goDataPtr)
*/
【结构体数据】转【json】
1 package main
2
3 import (
4 "fmt"
5 "encoding/json"
6
7 )
8
9
10 //结构体
11 type Person struct {
12 Name string
13 Age int
14 Rmb float64
15 Sex bool
16 Hobby []string
17 }
18
19
20
21
22 func main() {
23 Person :=Person{"小小",50,123.45,true,[]string{"吃","喝","玩"}}
24
25 bytes,err := json.Marshal(Person)
26 if err !=nil{
27 fmt.Println("序列化失败,err=",err)
28 return
29 }
30 fmt.Println(string(bytes)) //强制类型转换bytes转为string
31 }
32
33 /*
34 {"Name":"小小","Age":50,"Rmb":123.45,"Sex":true,"Hobby":["吃","喝","玩"]}
35 */
【map】转【json】
1 package main
2
3 import (
4 "fmt"
5 "encoding/json"
6
7 )
8
9 func main() {
10 dataMap :=make(map[string]interface{}) //stings做键,空接口做数值,任意类型。
11 dataMap["Name"]="小"
12 dataMap["Age"]=50
13 dataMap["Rmb"]=123.45
14 dataMap["Sex"]=true
15 dataMap["Hobby"]=[]string{"吃","喝","玩"}
16
17
18 bytes,err := json.Marshal(dataMap)
19 if err !=nil{
20 fmt.Println("序列化失败,err=",err)
21 return
22 }
23 fmt.Println(string(bytes)) //强制类型转换bytes转为string
24
25 }
26 /*
27 {"Age":50,"Hobby":["吃","喝","玩"],"Name":"小","Rmb":123.45,"Sex":true}
28 */
【切片】转【JSON】
1 package main
2
3 import (
4 "fmt"
5 "encoding/json"
6
7 )
8
9 func main() {
10
11 dataMap1 :=make(map[string]interface{}) //stings做键,空接口做数值,任意类型。
12 dataMap1["Name"]="小"
13 dataMap1["Age"]=50
14
15
16 dataMap2 :=make(map[string]interface{}) //stings做键,空接口做数值,任意类型。
17 dataMap2["Name"]="大"
18 dataMap2["Age"]=60
19
20 //将数据添加到切片中
21 dataSlice := make([]map[string]interface{},0)
22 dataSlice = append(dataSlice,dataMap1,dataMap2)
23
24
25 bytes,err := json.Marshal(dataSlice)
26 if err !=nil{
27 fmt.Println("序列化失败,err=",err)
28 return
29 }
30 fmt.Println(string(bytes)) //强制类型转换bytes转为string
31
32 }
33
34 /*
35 [{"Age":50,"Name":"小"},{"Age":60,"Name":"大"}]
36 */
【JSON】转【map】
1 package main
2
3 import (
4 "encoding/json"
5 "fmt"
6 )
7
8 func main() {
9 jsonStr := `{"Name":"小小","Age":50,"Rmb":123.45,"Sex":true,"Hobby":["吃","喝","玩"]}`
10 jsonBytes := []byte(jsonStr) //将字符串转为字节数
11 dataMap := make(map[string]interface{})
12 err := json.Unmarshal(jsonBytes, &dataMap) //注意这里是&dataMap
13 if err != nil {
14 fmt.Println("反序列化有错误:", err)
15 return
16 }
17 fmt.Println(dataMap)
18
19 }
20 /*
21 map[Age:50 Rmb:123.45 Sex:true Hobby:[吃 喝 玩] Name:小小]
22
23 */
【JSON】 转【结构体】
1 package main
2
3 import (
4 "encoding/json"
5 "fmt"
6 )
7
8 var jsonStr = `{"Name":"小小","Age":50,"Rmb":123.45,"Sex":true,"Hobby":["吃","喝","玩"]}`
9
10 func main() {
11
12 //定义结构体
13 type Person struct {
14 Name string
15 Age int
16 Rmb float64
17 Sex bool
18 Hobby []string
19 }
20
21 jsonBytes := []byte(jsonStr) //将字符串转为字节数
22 personPtr := new(Person)
23
24 err := json.Unmarshal(jsonBytes, personPtr)
25 if err != nil {
26 fmt.Println("转换有错误:", err)
27 return
28 }
29 fmt.Println(*personPtr)
30
31 }
32
33 /*
34 {小小 50 123.45 true [吃 喝 玩]}
35 */
【JSON】转【map切片】
1 package main
2
3 import (
4 "encoding/json"
5 "fmt"
6 )
7
8 func main() {
9
10 jsonStr := `[{"Name":"小小","Age":50,"Rmb":123.45,"Sex":true,"Hobby":["吃","喝","玩"]}]` //切片是[]开头
11 jsonBytes := []byte(jsonStr)
12 dataSlice := make([]map[string]interface{}, 0)
13
14 err := json.Unmarshal(jsonBytes, &dataSlice)
15 if err != nil {
16 fmt.Println("转换有错误:", err)
17 return
18 }
19 fmt.Println(dataSlice)
20
21 }
22 /*
23 [map[Age:50 Rmb:123.45 Sex:true Hobby:[吃 喝 玩] Name:小小]]
24 */
【JSON】转【结构体切片】
1 package main
2
3 import (
4 "encoding/json"
5 "fmt"
6 )
7
8 func main() {
9
10 //定义结构体
11 type Person struct {
12 Name string
13 Age int
14 Rmb float64
15 Sex bool
16 Hobby []string
17 }
18
19 jsonStr := `[{"Name":"小小","Age":50,"Rmb":123.45,"Sex":true,"Hobby":["吃","喝","玩"]}]`
20 jsonBytes := []byte(jsonStr)
21 persons := make([]Person, 0)
22
23 err := json.Unmarshal(jsonBytes, &persons)
24 if err != nil {
25 fmt.Println("转换有错误:", err)
26 return
27 }
28 fmt.Println(persons)
29
30 }
31 /*
32 [{小小 50 123.45 true [吃 喝 玩]}]
33 */
【编码映射】到【JSON文件】
1 package main
2
3 import (
4 "encoding/json"
5 "fmt"
6 "os"
7 )
8
9 func main() {
10
11 //go语言数据,此处为map
12 dataMap1 := make(map[string]interface{}) //stings做键,空接口做数值,任意类型。
13 dataMap1["Name"] = "小"
14 dataMap1["Age"] = 50
15
16 //创建并打开目标JSON文件
17 jsonFile, _ := os.OpenFile("C:/Go/xcy/src/check_list/main/b.json", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
18 defer jsonFile.Close()
19
20 //创建目标文件的编码器
21 decoder := json.NewEncoder(jsonFile)
22
23 //将GO语言数据编码到JSON文件,判断编码是否成功,成功自动会创建文件.
24 err := decoder.Encode(dataMap1)
25 if err != nil {
26 fmt.Println("转换有错误:", err)
27 return
28 }
29 fmt.Println("编码成功")
30
31 }
32
33 /*
34 cat a.json
35 {"Age":50,"Name":"小"}
36 */
37
38 // (in-sync)
39 //
【编码结构体切片】转【JSON文件】
1 package main
2
3 import (
4 "encoding/json"
5 "fmt"
6 "os"
7 )
8
9 func main() {
10
11 //结构体
12 type Person struct {
13 Name string
14 Age int
15 Rmb float64
16 Sex bool
17 Hobby []string
18 }
19
20 p1 := Person{"大大", 100, 100.234, true, []string{"a", "b", "c"}}
21 p2 := Person{"小小", 10, 100.234, true, []string{"a", "b", "c"}}
22 pepole := make([]Person, 0)
23 pepole = append(pepole, p1, p2)
24
25 //创建并打开目标JSON文件
26 jsonFile, _ := os.OpenFile("C:/Go/xcy/src/check_list/main/b.json", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
27 defer jsonFile.Close()
28
29 encoder := json.NewEncoder(jsonFile)
30 err := encoder.Encode(pepole)
31 if err != nil {
32 fmt.Println("转换有错误:", err)
33 return
34 }
35 fmt.Println("编码成功")
36
37 }
38 /*
39 cat b.json
40 [{"Name":"大大","Age":100,"Rmb":100.234,"Sex":true,"Hobby":["a","b","c"]},{"Name":"小小","Age":10,"Rmb":100.234,"Sex":true,"Hobby":["a","b","c"]}]
41 */
解码【JSON】 转 【MAP】
1 package main
2
3 import (
4 "encoding/json"
5 "fmt"
6 "os"
7 )
8
9 func main() {
10
11 //创建并打开目标JSON文件
12 srcFile, _ := os.Open("C:/Go/xcy/src/check_list/main/b.json")
13 defer srcFile.Close()
14
15 //创建map,用与接受解码好的数据
16 dataMap := make(map[string]interface{})
17
18 //创建源文件的解码器
19 decoder := json.NewDecoder(srcFile)
20
21 //解码源文件中的数据,丢入dataMap
22 err := decoder.Decode(&dataMap)
23 if err != nil {
24 fmt.Println("解码有错误:", err)
25 return
26 }
27 fmt.Println("编码成功",dataMap)
28
29 }
30 /*
31 结果:
32 编码成功 map[Age:100 Rmb:100.234 Sex:true Hobby:[a b c] Name:大大]
33
34 cat b.json
35 {"Name":"大大","Age":100,"Rmb":100.234,"Sex":true,"Hobby":["a","b","c"]},{"Name":"小小","Age":10,"Rmb":100.234,"Sex":true,"Hobby":["a","b","c"]}
36
37 */
解码JSON文件为结构体切片
1 package main
2
3 import (
4 "encoding/json"
5 "fmt"
6 "os"
7 )
8
9 func main() {
10
11 //结构体
12 type Person struct {
13 Name string
14 Age int
15 Rmb float64
16 Sex bool
17 Hobby []string
18 }
19 //创建并打开目标JSON文件
20 srcFile, _ := os.Open("C:/Go/xcy/src/check_list/main/b.json")
21 defer srcFile.Close()
22
23 //创建用于接收数据的变量.
24 people := make([]Person, 0)
25
26 //创建源文件的解码器
27 decoder := json.NewDecoder(srcFile)
28
29 //解码源文件中的数据,丢入dataMap
30 err := decoder.Decode(&people)
31 if err != nil {
32 fmt.Println("解码有错误:", err)
33 return
34 }
35 fmt.Println("编码成功", people)
36
37 }
38 /*
39 结果:
40 编码成功 [{大大 100 100.234 true [a b c]} {小小 10 100.234 true [a b c]}]
41
42 cat b.json
43 [{"Name":"大大","Age":100,"Rmb":100.234,"Sex":true,"Hobby":["a","b","c"]},{"Name":"小小","Age":10,"Rmb":100.234,"Sex":true,"Hobby":["a","b","c"]}]
44
45 */