go json
json数据协议
导入包: Import “encoding/json”
序列化: json.Marshal(data interface{})
反序列化: json.UnMarshal(data []byte, v interface{})
json序列化结构体、序列化map、序列化slice示例:
package main import ( "encoding/json" "fmt" ) type User struct { UserName string `json:"username"` NickName string `json:"nickname"` Age int Birthday string Sex string Email string Phone string } func testStruct() { user1 := &User{ UserName: "user1", NickName: "学习", Age: 18, Birthday: "2008/8/8", Sex: "男", Email: "shhnwangjian@qq.com", Phone: "13912345678", } data, err := json.Marshal(user1) if err != nil { fmt.Printf("json.marshal failed, err:", err) return } fmt.Printf("%s\n", string(data)) } func testInt() { var age = 100 data, err := json.Marshal(age) if err != nil { fmt.Printf("json.marshal failed, err:", err) return } fmt.Printf("%s\n", string(data)) } func testMap() { 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 { fmt.Printf("json.marshal failed, err:", err) return } fmt.Printf("%s\n", string(data)) } func testSlice() { var m map[string]interface{} var s []map[string]interface{} m = make(map[string]interface{}) m["username"] = "user1" m["age"] = 18 m["sex"] = "man" s = append(s, m) m = make(map[string]interface{}) m["username"] = "user2" m["age"] = 29 m["sex"] = "female" s = append(s, m) data, err := json.Marshal(s) if err != nil { fmt.Printf("json.marshal failed, err:", err) return } fmt.Printf("%s\n", string(data)) } func main() { testStruct() fmt.Println("-----------------------") testInt() fmt.Println("-----------------------") testMap() fmt.Println("-----------------------") testSlice() }
反序列化示例
package main import ( "encoding/json" "fmt" ) type User struct { UserName string `json:"username"` NickName string `json:"nickname"` Age int Birthday string Sex string Email string Phone string } func testStruct() (ret string, err error) { user1 := &User{ UserName: "user1", NickName: "上课", Age: 18, Birthday: "2008/8/8", Sex: "男", Email: "sh@qq.com", Phone: "1234567890", } data, err := json.Marshal(user1) if err != nil { err = fmt.Errorf("json.marshal failed, err:", err) return } ret = string(data) return } 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:", err) return } ret = string(data) return } func test2() { 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(m) } func test() { 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(user1) } func main() { test() test2() }