json 编解码
ORM 映射关系
``package main import ( "encoding/json" "fmt" ) type Movie struct { Title string `json:"title"` Year int `json:"year"` Price int `json:"rmb"` Actors []string `json:"actors"` } func main() { movie := Movie{"喜剧之王", 2000, 10, []string{"xingye", "zhangbozhi"}} // 编码过程 struct -> json jsonStr, err := json.Marshal(movie) if err != nil { fmt.Println("json marsha1 error ", err) } fmt.Printf("jsonStr = %s\n", jsonStr) } ``
image
``package main import ( "encoding/json" "fmt" ) type Movie struct { Title string `json:"title"` Year int `json:"year"` Price int `json:"rmb"` Actors []string `json:"actors"` } func main() { movie := Movie{"喜剧之王", 2000, 10, []string{"xingye", "zhangbozhi"}} // 编码过程 struct -> json jsonStr, err := json.Marshal(movie) if err != nil { fmt.Println("json marsha1 error ", err) } fmt.Printf("jsonStr = %s\n", jsonStr) // 解码过程 json -> struct // jsonStr = {"title":"喜剧之王","year":2000,"rmb":10,"actors":["xingye","zhangbozhi"]} myMovie := Movie{} err = json.Unmarshal(jsonStr, &myMovie) if err != nil { fmt.Println("json unmarsha1 error ", err) return } fmt.Printf("%v\n", myMovie) } ``
image
map 是 Golang 中的字典类型,key 是 string 类型,interface{} 是 Golang 中的万能接口类型。
``package main import ( "encoding/json" "fmt" ) type Movie struct { Title string `json:"title"` Year int `json:"year"` Price int `json:"rmb"` Actors []string `json:"actors"` } func main() { movie := Movie{"喜剧之王", 2000, 10, []string{"xingye", "zhangbozhi"}} // 编码过程 struct -> json jsonStr, err := json.Marshal(movie) if err != nil { fmt.Println("json marsha1 error ", err) } fmt.Printf("jsonStr = %s\n", jsonStr) // 解码过程 json -> struct // jsonStr = {"title":"喜剧之王","year":2000,"rmb":10,"actors":["xingye","zhangbozhi"]} myMovie := Movie{} err = json.Unmarshal(jsonStr, &myMovie) if err != nil { fmt.Println("json unmarsha1 error ", err) return } fmt.Printf("%v\n", myMovie) fmt.Printf("jsonStr's type is: %T\n", jsonStr) // 解码过程 json -> map[string]interface{} var vul map[string]interface{} vulJsonStr := `{ "urls": [ "https://snyk-patches.s3.amazonaws.com/npm/backbone/20110701/backbone_20110701_0_0_0cdc525961d3fa98e810ffae6bcc8e3838e36d93.patch" ], "version": "<0.5.0 >=0.3.3", "modificationTime": "2015-11-06T02:09:36.180Z", "comments": [ "https://github.com/jashkenas/backbone/commit/0cdc525961d3fa98e810ffae6bcc8e3838e36d93.patch" ], "id": "patch:npm:backbone:20110701:0" }` fmt.Printf("vulJsonStr's type is %T\n", vulJsonStr) vulJsonByteSlice := []byte(vulJsonStr) err = json.Unmarshal(vulJsonByteSlice, &vul) if err != nil { fmt.Println("json unmarshal error ", err) return } fmt.Printf("%v\n", vul) fmt.Println("vul version is: ", vul["version"]) } ``
image
interface{} 是 Golang 中的万能类型,当我们不知道 json 的具体格式时,或者 json 的格式是变化的时,我们可以将其转换为 interface{} 万能类型,此时我们可以看到 json 最后的格式为 map[string]interface{}。
``package main import ( "encoding/json" "fmt" ) type Movie struct { Title string `json:"title"` Year int `json:"year"` Price int `json:"rmb"` Actors []string `json:"actors"` } func main() { movie := Movie{"喜剧之王", 2000, 10, []string{"xingye", "zhangbozhi"}} // 编码过程 struct -> json jsonStr, err := json.Marshal(movie) if err != nil { fmt.Println("json marsha1 error ", err) } fmt.Printf("jsonStr = %s\n", jsonStr) // 解码过程 json -> struct // jsonStr = {"title":"喜剧之王","year":2000,"rmb":10,"actors":["xingye","zhangbozhi"]} myMovie := Movie{} err = json.Unmarshal(jsonStr, &myMovie) if err != nil { fmt.Println("json unmarsha1 error ", err) return } fmt.Printf("%v\n", myMovie) fmt.Printf("jsonStr's type is: %T\n", jsonStr) // 解码过程 json -> map[string]interface{} var vul map[string]interface{} vulJsonStr := `{ "urls": [ "https://snyk-patches.s3.amazonaws.com/npm/backbone/20110701/backbone_20110701_0_0_0cdc525961d3fa98e810ffae6bcc8e3838e36d93.patch" ], "version": "<0.5.0 >=0.3.3", "modificationTime": "2015-11-06T02:09:36.180Z", "comments": [ "https://github.com/jashkenas/backbone/commit/0cdc525961d3fa98e810ffae6bcc8e3838e36d93.patch" ], "id": "patch:npm:backbone:20110701:0" }` fmt.Printf("vulJsonStr's type is %T\n", vulJsonStr) vulJsonByteSlice := []byte(vulJsonStr) err = json.Unmarshal(vulJsonByteSlice, &vul) if err != nil { fmt.Println("json unmarshal error ", err) return } fmt.Printf("%v\n", vul) fmt.Println("vul version is: ", vul["version"]) fmt.Printf("%T\n", vul) fmt.Println("====================") var vulIF interface{} err = json.Unmarshal(vulJsonByteSlice, &vulIF) if err != nil { fmt.Println("json unmarshal error ", err) return } fmt.Println(vulIF) fmt.Printf("%T", vulIF) } ``
image
[Go]当把json解析到interface{}时 , 对应的真正类型 https://www.cnblogs.com/taoshihan/p/12960449.html