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 := json.Marshal(m)//result = {"...
"fmt" ) 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...
m1 := map[string]interface{}{"name": "John", "age": 10} m2 := map[string]interface{}{"name": "Alex", "age": 12} s = append(s, m1, m2) s = append(s, m2) b, err := json.Marshal(s) if err != nil { fmt.Println("json.Marshal failed:", err) return } fmt.Println...
三.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 [string] interface {} 存储 JOSN 对象 [] interface 存储 JOSN 数组 1. 2. json.Unmarshl 将会把任何合法的 JSON 数据存储到一个 interface {} 类型的值,通过使用空接口类型我们可以存储任意值,但是使用这种类型作为值时需要先做一次类型断言。
beforeMap := map[string]interface {}{ "id":"123", "user_name":"酒窝猪", "address":[]map[string]interface{}{{"address": "address01"}, {"address": "address02"}}, } var afterStruct =UserInfoVo{} before := time.Now() marshal, err := json.Marshal(beforeMap) ...
上面的代码中,我们定义了一个名为infoMap的map类型,并初始化了其内部的数据。我们将map的键设置为string类型,值则设为interface{}类型,以便存储一个任意类型的数据。 接下来,我们使用"encoding/json"包中的Marshal方法将map数据转化为JSON格式字符串。 //将map数据转化为JSON格式字符串jsonBytes, err := json.Mars...
// 强转interface类型到string类型(注意: 不是 convert.ToJSONString)wordCloudJson:=convert.ToString(data[0]["word_cloud_json"])words:=make(map[string]interface{})err=json.Unmarshal([]byte(wordCloudJson),&words)iferr!=nil{logu.CtxError(ctx,error_code.ProcessError,"GetBrandWordCloud Unmarshal",...
//自动转map fmt.Println(i) //4.使用interface的json,可以判断类型 m := i.(map[string]interface{}) for k, v := range m { switch value := v.(type) { case nil: fmt.Println(k, "is nil", "null") case string: fmt.Println(k, "is string", value) case int: fmt.Println(k, "...