package main
import (
"encoding/json"
"fmt"
)
type User struct {
ID uint64
Name string
Age uint32
}
// 结构体转字符串
func main() {
user := User{104215857142, "Sky", 20}
u, err := json.Marshal(user)
if err != nil {
fmt.Println("Marshal failed:", err)
return
}
fmt.Println("用户信息由结构体转为json:", string(u))
}
结果信息为:
用户信息由结构体转为json: {"ID":104215857142,"Name":"Sky","Age":20}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)