1 回答
TA贡献1821条经验 获得超6个赞
来自包的Golang文档json
package main
import (
"encoding/json"
"fmt"
)
func main() {
type ColorGroup struct {
ID int `json:"id"`
Name string `json:"name"`
Colors []string `json:"colors"`
}
group := ColorGroup{
ID: 1,
Name: "Reds",
Colors: []string{"Crimson", "Red", "Ruby", "Maroon"},
}
b, err := json.Marshal(group)
if err != nil {
fmt.Println("error:", err)
} else {
str := string(b)
fmt.Println("stringified json is:", str)
}
}
Output
stringified json is: {"id":1,"name":"Reds","colors":["Crimson","Red","Ruby","Maroon"]}
返回json.Marshal两个值 - abyte array和error Iferror则可以通过转换为使用 来nil获取字符串byte arraystringstr := string(b)
- 1 回答
- 0 关注
- 96 浏览
添加回答
举报