golang生成json

package main

import (
	"encoding/json"
	"fmt"
)

func main()  {
	// 继承
	person := person{Id: 22, Name: "kit"}
	student := student{person: person, ClassId: 1, ClassName: "236"}
	fmt.Print("json前:","\n",student)

	// 转json
	jsonByte, err := json.Marshal(student)
	if err == nil {
		fmt.Print("\njson后:","\n",string(jsonByte))
	}
}

type person struct{
	Id int
	Name string
}

type student struct {
	person // 继承
	ClassId int
	ClassName string
}

输出:

D:\develop\go_path\src\go.xushiwei.note>go run test.go
json前:
{{22 kit} 1 236}
json后:
{"Id":22,"Name":"kit","ClassId":1,"ClassName":"236"}
发布了65 篇原创文章 · 获赞 38 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/HelloWorldYangSong/article/details/103799713
今日推荐