m :=map[string]string{"type":"10","msg":"hello."} mjson,_ :=json.Marshal(m) mString :=string(mjson) fmt.Printf("print mString:%s",mString)
func MapToJson(param map[string]interface{}) string {//json转map dataType, _ :=json.Marshal(param) dataString :=string(dataType)returndataString } func JsonToMap(str string) map[string]interface{} {//map 转json var tempMap map[string]interface{} err := json.Unmarshal([]byte(str), &...
func main() { m := make(map[string]interface{}, 4) m["name"] = "wilson" m["lang"] = []string{"go", "python", "java"} m["isok"] = true m["price"] = 5.55 result, err := json.Marshal(m) if err != nil { return } fmt.Println("result = ", string(result)) } 2....
先将map转换成JSON,再通过 JSON 转换成struct 操作有点繁琐 func TestMapToStructByJson(t *testing.T) { beforeMap := map[string]interface {}{ "id":"123", "user_name":"酒窝猪", "address":[]map[string]interface{}{{"address": "address01"}, {"address": "address02"}}, ...
三.map转json packagemainimport("encoding/json""fmt")funcmain(){m:=make(map[string]interface{})m["name"]="HanMeimei"m["age"]=11m["gender"]='M'm["subject"]=[]string{"语文","数学","英语"}fmt.Println("m = ",m)mJson,err:=json.Marshal(m)iferr!=nil{fmt.Println(err)return}fmt...
//将map数据转化为JSON格式字符串jsonBytes, err := json.Marshal(infoMap)//判断是否转换成功iferr !=nil{ fmt.Println("JSON格式转换失败,错误信息为:", err)return}//打印JSON格式数据fmt.Println(string(jsonBytes)) AI代码助手复制代码 执行以上代码,得到以下输出: ...
PhoneNumber string `json:"phone_number"` Hobbies []string `json:"hobbies"` } // 对于带 下划线命名 的变量,不能直接解析到 func TestRangeMap1(t *testing.T) { m1 := map[string]interface{}{ "name": "whw", "phone_number": "13333333333", // TODO 将字段设置为 phonenumber 就可以解析了...
通过map生成json packagemainimport("encoding/json""fmt")funcmain(){student:=make(map[string]interface{})student["name"]="5lmh.com"student["age"]=18student["sex"]="man"b,err:=json.Marshal(student)iferr!=nil{fmt.Println(err)}fmt.Println(b)} ...
import ("encoding/json""fmt") func main() {//创建一个mapm := make(map[string]interface{},4)//因为类型多,可以用interface空接口m["address"] ="北京"m["languages"] = []string{"Golang","PHP","Java","Python"} m["status"] =truem["price"] =666.666//编码成json//result, err :=...