咔咔博客之map转json
跟结构体转json同样都使用的是json.Marshal()方法 最后须要就是把字节转为字符串使用string便可json
案例
func main() { // 定义了interface 后边就能够跟任意类型了 mMap := make(map[string]interface{ }) mMap["博客地址"] = "blog.fangkang.top" mMap["age"] = 21 bytes, e := json.Marshal(mMap) if e != nil { fmt.Printf("序列化失败") return } else { jsonStr := string(bytes) fmt.Println(jsonStr) } }