go 语言 生成json字符串数组

    技术2022-07-10  106

    实现

    https://blog.csdn.net/zxy_666/article/details/80173288

    Json Marshal:将数据编码成json字符串

     

    package main import( "fmt" "encoding/json" ) type Stu struct { Name string `json:"name"` Age int HIgh bool sex string Class *Class `json:"class"` } type Class struct { Name string Grade int } func main() { //实例化一个数据结构,用于生成json字符串 stu := Stu{ Name: "张三", Age: 18, HIgh: true, sex: "男", } //指针变量 cla := new(Class) cla.Name = "1班" cla.Grade = 3 stu.Class=cla //Marshal失败时err!=nil jsonStu, err := json.Marshal(stu) if err != nil { fmt.Println("生成json字符串错误") } fmt.Println(string(jsonStu)) var a = [3]Stu{stu, stu, stu} jsonStu2, err2 := json.Marshal(a) if err2 != nil { fmt.Println("生成json字符串错误") } //jsonStu是[]byte类型,转化成string类型便于查看 fmt.Println(string(jsonStu2)) }

    Go的json解析:Marshal与Unmarshal

    Processed: 0.013, SQL: 9