分别对struct、slice、基本类型、map进行序列化和反序列化:
package main import ( "encoding/json" "fmt" ) type User struct { UserName string `json:"username"` //改变json打包的key,将大写变成小写 NickName string `json:"nickname"` Age int `json:"age"` Birthday string `json:"birthday"` Sex string `json:"sex"` Email string `json:"email"` Phone string `json:"phone"` } //结构体进行序列化 func testStruct() (ret string, err error) { user1 := &User{ UserName: "user1", NickName: "shangke", Age: 18, Birthday: "2008/8/8", Sex: "mael", Email: "xiehzh@ffcs.cn", Phone: "18950200000", } //fmt.Println("before marshal: ", user1) data, err := json.Marshal(user1) if err != nil { err = fmt.Errorf("json.marshal failed,err:%v", err) return } //fmt.Println("after marshal: ", data) ret = string(data) //fmt.Println("json format--> string(struct): ", ret) return } //变量进行序列化 func testInt() (ret string, err error) { var age = 100 data, err := json.Marshal(age) if err != nil { err = fmt.Errorf("json.marshal failed,err:%v", err) return } ret = string(data) return } //map进行序列化 func testMap() (ret string, err error) { var m map[string]interface{} m = make(map[string]interface{}) m["username"] = "user1" m["age"] = 18 m["sex"] = "man" data, err := json.Marshal(m) if err != nil { err = fmt.Errorf("json.marshal failed,err:%v", err) return } ret = string(data) return } //对slice进行序列化,slice里面可以是struct,基本类型,也可以是map。{这个相当复杂} func testSlice() (ret string, err error) { var s []map[string]interface{} var m map[string]interface{} m = make(map[string]interface{}) m["username"] = "user1" m["age"] = 18 m["sex"] = "male" s = append(s, m) m["username"] = "user2" m["age"] = 22 m["sex"] = "female" s = append(s, m) data, err := json.Marshal(s) if err != nil { err = fmt.Errorf("json.marshal failed,err:%v", err) return } ret = string(data) return } //反序列化struct测试 func UnmsStruct() { data, err := testStruct() if err != nil { fmt.Println("test struct failed,", err) return } var user1 User err = json.Unmarshal([]byte(data), &user1) if err != nil { fmt.Println("Unmarshal failed, ", err) return } fmt.Println("UnMarshal: ", user1) } //反序列化Map测试 func UnmsMap() { data, err := testMap() if err != nil { fmt.Println("test map failed,", err) return } var m map[string]interface{} err = json.Unmarshal([]byte(data), &m) if err != nil { fmt.Println("Unmarshal failed, ", err) return } fmt.Println("UnMarshal: ", m) } //反序列化Slice测试 func UnmsSlice() { data, err := testSlice() if err != nil { fmt.Println("test map failed,", err) return } var s []map[string]interface{} err = json.Unmarshal([]byte(data), &s) if err != nil { fmt.Println("Unmarshal failed, ", err) return } fmt.Println("UnMarshal: ", s) } //反序列化Int测试 func UnmsInt() { data, err := testInt() if err != nil { fmt.Println("test map failed,", err) return } var i int err = json.Unmarshal([]byte(data), &i) if err != nil { fmt.Println("Unmarshal failed, ", err) return } fmt.Println("UnMarshal: ", i) } func main() { fmt.Println("==============结构体反序列化==============") UnmsStruct() fmt.Println("==============map反序列化==============") UnmsMap() fmt.Println("==============slice反序列化==============") UnmsSlice() fmt.Println("==============基本类型反序列化==============") UnmsInt() }
output
PS F:\go> .\main.exe ==============结构体反序列化============== UnMarshal: {user1 shangke 18 2008/8/8 mael xiehzh@ffcs.cn 18950200000} ==============map反序列化============== UnMarshal: map[age:18 sex:man username:user1] ==============slice反序列化============== UnMarshal: [map[age:22 sex:female username:user2] map[age:22 sex:female username:user2]] ==============基本类型反序列化============== UnMarshal: 100 PS F:\go>
作者:XieHZ同学