package to operate JSON data.
(1) Use the json.Marshal() method to convert the structure into a JSON string
Print: After serialization: {"Name": "tom", "Age": 12, "Skill": "football"}(2) JSON To convert a string into a structure, you can use the json.Unmarshal() method
to print: After deserialization: Student={tom 12 football} , Name=tom
(3) How to realize that the name of the key can be customized after the structure is serialized
For the name of the customized key, you can specify it in the struct variable After printing a , you can see that the name of the key has changed to the name of the tag label we specified After serialization: {"stu_name":"tom ","stu_age":12,"Skill":"football"}
func main() { // map 转 Json字符串 m := make(map[string]interface{}) m["name"] = "jetty" m["age"] = 16 data, err := json.Marshal(&m) if err != nil { fmt.Printf("序列化错误 err=%v\n", err) return } fmt.Println("序列化后: ", string(data)) // 打印: 序列化后: {"age":16,"name":"jetty"} // Json字符串 转 map str := `{"age":25,"name":"car"}` err = json.Unmarshal([]byte(str), &m) if err != nil { fmt.Printf("反序列化错误 err=%v\n", err) return } fmt.Printf("反序列化后: map=%v, name=%v\n", m, m["name"]) // 打印: 反序列化后: map=map[age:25 name:car], name=car}
- If the first letter of the variable is lowercase, it is private. Because the reflection information cannot be obtained, it cannot be transferred.
- If the first letter of the variable is capitalized, it is public. It can be converted normally regardless of whether tags are added or not. Variables with tags will be displayed according to the name of the tag.
If the first letter is lowercase, it cannot be converted to json data regardless of whether it is added with a tag, while if it is uppercase, it can be aliased with a tag, and if it is not added with a tag, it will be converted into json data. The fields are consistent with the original names of the structure variables
type User struct { Name string `json:"u_name"` Password string `json:"password"` Email string `json:"email"`}func main() { user := User{"admin", "pwd", "user@163.com"} person := Person{23, "上海"} // 忽略掉 Password 字段 data, _ := json.Marshal(struct { *User Password string `json:"password,omitempty"` }{User: &user}) fmt.Println("忽略字段: ", string(data)) // 打印: 忽略字段: {"u_name":"admin","email":"user@163.com"}}Ignore fields: {"u_name":"admin","email":"user@163.com"}}
data, _ = json.Marshal(struct { *User Skill string `json:"skill"` // 临时添加额外的 Skill字段}{ User: &user, Skill: "football",})fmt.Println("添加额外字段: ", string(data))Add additional fields: {"u_name":"admin","password":"pwd","email":"user@163.com","skill":"football"}
type User struct { Name string `json:"u_name"` Password string `json:"password"` Email string `json:"email"`}type Person struct { Age int Addr string `json:"addr"`}func main() { // 初始化两个 struct user := User{"admin", "pwd", "user@163.com"} person := Person{23, "上海"} data, _ := json.Marshal(struct { *User *Person }{ User: &user, Person: &person, }) fmt.Println("合并两个struct: ", string(data))}Merge two structs: {"u_name":"admin","password":"pwd","email" : "user@163.com", "Age": 23, "addr": "Shanghai"}
emp := struct { // 创建匿名 struct Num int `json:"num,string"`}{15,}data, _ := json.Marshal(&emp)fmt.Println("数字转成字符串: ", string(data)) // 数字转成字符串: {"num":"15"}str := `{"Num":"25"}`_ = json.Unmarshal([]byte(str), &emp)fmt.Printf("字符串转成数字: Emp.Num=%v\n", emp.Num) // 字符串转成数字: Emp.Num=25
str = ` {"u_name":"system","password":"abc","email":"user2@163.com","Age":23,"addr":"杭州"}`var user2 Uservar person2 Person_ := json.Unmarshal([]byte(str), &struct { *User *Person}{ User: &user2, Person: &person2,})fmt.Printf("分成两个struct: User=%v, Person=%v\n", user2, person2)is divided into two structs: User={system abc user2@163.com}, Person={23 Hangzhou}