Golang中json如何使用
更新:HHH   时间:2023-1-7


本篇文章为大家展示了Golang中json如何使用,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

1,结构体解析为json

type CodeType struct {
Name string
}
func main() {
codeType := CodeType{
Name: "Golang",
}
jsonInfo, err := json.Marshal(codeType)
if err != nil {
fmt.Println( err)
}
os.Stdout.Write(jsonInfo)
}

输出结果为:

{"Name":"Golang"}

2.json解析为结构体

type CodeType struct {
Name string
}
func main() {

var jsonInfo =[]byte(`[
{"Name":"Golang"},{"Name":"Python"}
]`)

var codeType  []CodeType
err :=json.Unmarshal(jsonInfo,&codeType)
if err !=nil {
fmt.Println(err)

}
fmt.Printf("%+v",codeType)
}

输出结果为:

[{Name:Golang} {Name:Python}]

上述内容就是Golang中json如何使用,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注天达云行业资讯频道。

返回大数据教程...