ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[TOC] **JSON**(JavaScript Object Notation)是一种轻量级的数据交换格式,可使人们很容易地进行阅读和编写,同时也方便了机器进行解析和生成。JSON适用于进行数据交互的场景,如网站前台与后台之间的数据交互,不同语言之间数据传递,数据保存到文件中等等... ## 序列化与反序列化 ```go // Student 是学生的结构体 type Student struct { Sid int Name string Age int8 } // Class 是班级的结构体 type Class struct { Title string Student []*Student } // NewStudent 是 Student 结构体的构造函数 func NewStudent(sid int, name string, age int8) *Student { return &Student{ Sid: sid, Name: name, Age: age, } } func main() { s1 := NewStudent(1, "jiaxzeng", 22) s2 := NewStudent(2, "xiaodunan", 18) c1 := Class{"Cloud computing", []*Student{s1, s2}} fmt.Printf("c1序列化前: %#v\n", c1) // json序列化 // 传任何类型的数据,返回byte类型和error date, err := json.Marshal(c1) if err == nil { fmt.Println("c1序列化byte:", date) fmt.Println("c1序列化string:", string(date)) } else { fmt.Println("json marshal failed, err:", err) } // json反序列化 var c2 Class err = json.Unmarshal(date, &c2) if err == nil { fmt.Printf("c2反序列化: %#v\n", c2) } else { fmt.Println("json unmarshal failed, err:", err) } } // 运行结果 // c1序列化前: main.Class{Title:"Cloud computing", Student:[]*main.Student{(*main.Student)(0xc0000be000), (*main.Student)(0xc0000be020)}} // c1序列化byte: [123 34 84 105 116 108 101 34 58 34 67 108 111 117 100 32 99 111 109 112 117 116 105 110 103 34 44 34 83 116 117 100 101 110 116 34 58 91 123 34 83 105 100 34 58 49 44 34 78 97 109 101 34 58 34 106 105 97 120 122 101 110 103 34 44 34 65 103 101 34 58 50 50 125 44 123 34 83 105 100 34 58 50 44 34 78 97 109 101 34 58 34 120 105 97 111 100 117 110 97 110 34 44 34 65 103 101 34 58 49 56 125 93 125] // c1序列化string: {"Title":"Cloud computing","Student":[{"Sid":1,"Name":"jiaxzeng","Age":22},{"Sid":2,"Name":"xiaodunan","Age":18}]} // c2反序列化: main.Class{Title:"Cloud computing", Student:[]*main.Student{(*main.Student)(0xc0000be0a0), (*main.Student)(0xc0000be0e0)}} ``` >[warning] `注意` > - 结构体成员变量是小写的话,序列化后是看不到这个字段的 > - 结构体名称是小写,是可以正常序列化 ## 结构体标签(Tag) 从上面的示例可知,序列化后的变量名称也是大写的。需求序列化后变量名称是小写的。 在结构体定义上设计标签即可,例如由下示例: ```go // Student 是学生的结构体 type Student struct { Sid int `json:"id"` Name string `json:"name"` Age int8 `json:"age"` } // Class 是班级的结构体 type Class struct { Title string Student []*Student } // NewStudent 是 Student 结构体的构造函数 func NewStudent(sid int, name string, age int8) *Student { return &Student{ Sid: sid, Name: name, Age: age, } } func main() { s1 := NewStudent(1, "jiaxzeng", 22) s2 := NewStudent(2, "xiaodunan", 18) c1 := Class{"Cloud computing", []*Student{s1, s2}} fmt.Printf("c1序列化前: %v\n", c1) // json序列化 // 传任何类型的数据,返回byte类型和error date, err := json.Marshal(c1) if err == nil { // fmt.Println("c1序列化byte:", date) fmt.Println("c1序列化后string:", string(date)) } else { fmt.Println("json marshal failed, err:", err) } // json反序列化 var c2 Class err = json.Unmarshal(date, &c2) if err == nil { fmt.Printf("c2反序列化后: %v\n", c2) } else { fmt.Println("json unmarshal failed, err:", err) } } // 运行结果 // c1序列化前: {Cloud computing [0xc0000be000 0xc0000be020]} // c1序列化后string: {"Title":"Cloud computing","Student":[{"id":1,"name":"jiaxzeng","age":22},{"id":2,"name":"xiaodunan","age":18}]} // c2反序列化后: {Cloud computing [0xc0000be0a0 0xc0000be0e0]} ```