🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
``` package main import ( "encoding/json" "fmt" ) type Monster struct {     Name string `json:"name1"`// 使用别名,转换后为别名,可以将首字母大写的字段转换为小写,否则小写无法输出     Age      int     Birthday string     Sal      float64 } func testStruct() {     m := Monster{         Name: "牛魔王",         Age:      60,         Birthday: "2022-01-02",         Sal:      200.22,     }     data, err := json.Marshal(&m) //\[\]byte     if err != nil {         fmt.Printf("序列化错误,err=%v\n", err)     }     fmt.Printf("monster序列化后=%v\n", string(data)) } func testMap() {     var a map[string]interface{}     a = make(map[string]interface{})     a["name"] = "红孩儿"     a["age"] = "30"     data, err := json.Marshal(a) //[]byte     if err != nil {         fmt.Printf("序列化错误,err=%v\n", err)     }     fmt.Printf("monster序列化后=%v\n", string(data)) } func testSlice() {     var slice []map[string]interface{}     var m1 map[string]interface{}     m1 = make(map[string]interface{})     m1["name"] = "红孩儿"     m1["age"] = "30"     slice = append(slice, m1)     var m2 map[string]interface{}     m2 = make(map[string]interface{})     m2["name"] = "牛魔王"     m2["age"] = "4399"     slice = append(slice, m2)     data, err := json.Marshal(slice) //\[\]byte     if err != nil {         fmt.Printf("序列化错误,err=%v\n", err)     }     fmt.Printf("monster序列化后=%v\n", string(data)) } func main() { testStruct() testMap() testSlice() } ``` ![](https://img.kancloud.cn/0a/84/0a8428a48c0db8f41aebf06a22a18eb2_588x67.png)