编码JSON
编码JSON,可以将map类型或结构类型的值转换为JSON文档。序列化(marshal)是指将数据转换为JSON字符串的过程
示例代码如下:
package main import ( "encoding/json" "fmt" "log" ) func main() { c := make(map[string]interface{}) c["name"] = "Gopher" c["title"] = "programmer" c["contact"] = map[string]interface{}{ "home": "415.332.23", "cell": "415.568.25", } data, err := json.MarshalIndent(c, "", " ") if err != nil { log.Println("error:", err) return } fmt.Println(string(data)) }