go json.Marshal 和 json.Unmarshal与结构体
结构体属性名字小写会被忽略
package main import ( "encoding/json" "fmt" ) type People struct { name string `json:"name"` Height string `json:"height"` } type Student struct { Age string `json:"age"` weight string `json:"weight"` } func main() { s := Student{ Age: "13", weight: "33", //属性小写,在使用Marshal转换成切片的时候会被忽略 } if sslice, err := json.Marshal(s); err == nil { fmt.Println(string(sslice)) // {"age":"13"} 忽略了小写的weight属性 } js := `{ "name":"11", "Height":"179" }` var p People err := json.Unmarshal([]byte(js), &p) if err != nil { fmt.Println("err :", err) return } fmt.Println("people:", p) //people: { 179} 忽略了小写name 属性 }
posted on 2022-10-17 11:03 running-fly 阅读(226) 评论(0) 收藏 举报
浙公网安备 33010602011771号