Golang 将 interface{} 转换为 JSON 字符串
可以使用标准库中的 json.Marshal 函数将 interface{} 类型转换成 JSON 字符串:
import "encoding/json"
func main() {
data := map[string]interface{} {
"name": "John",
"age": 30,
"married": true,
}
jsonStr, err := json.Marshal(data)
if err != nil {
panic(err)
}
fmt.Println(string(jsonStr))
}
输出结果:
{"age":30,"married":true,"name":"John"}
原文地址: https://www.cveoy.top/t/topic/lNga 著作权归作者所有。请勿转载和采集!