Golang 编码和解析Json
🎈 作者:Linux猿
🎈 简介:CSDN博客专家🏆,华为云享专家🏆,Linux、C/C++、面试、刷题、算法尽管咨询我,关注我,有问题私聊!
🎈 欢迎小伙伴们点赞👍、收藏⭐、留言💬
在 Golang encoding/json 包中提供了两个函数 Marshal 和 Unmarshal,分别用来编码和解码(解析)Json 格式的数据。
一、编码 :func Marshal
func Marshal(v interface{}) ([]byte, error)
作用:返回 v 编码后的 Json 数据,注意是 []byte,例如:
package main
import (
"encoding/json"
"fmt"
)
type Students struct {
Name string `Json:"name"`
Height int `Json:"height"`
Sex string `Json:"sex"`
}
type Teachers struct {
Name string `Json:"name"`
Height int `Json:"height"`
Sex string `Json:"sex"`
}
type School struct {
Name string `Json:"name"`
Area int `Json:"area"`
Student []Students `Json:"student"`
Teacher []Teachers `Json:"teacher"`
}
//initAssignment 初始化School
func initAssignment() School {
student := []Students{
{"张三", 180, "男"},
{"王丽", 165, "女"},
}
teacher := []Teachers{
{"李四", 180, "女"},
{"王五", 177, "男"},
}
school := School{"中关村大学", 1024, student, teacher}
return school
}
func main() {
school := initAssignment()
schoolJson, err := json.Marshal(school)
if err != nil {
fmt.Println("Umarshal Error:" + err.Error())
return
}
fmt.Println("schoolJson:", string(schoolJson))
}
在上面的实例中,有三个结构 School、Students 以及 Teacher,其中,School 包含后面两者,实例化一个 School 类型,并将其转化为 Json 类型。
输出:
[root@localhost gotest]# go run main.go
schoolJson: {"Name":"中关村大学","Area":1024,"Student":[{"Name":"张三","Height":180,"Sex":"男"},{"Name":"王丽","Height":165,"Sex":"女"}],"Teacher":[{"Name":"李四","Height":180,"Sex":"女"},{"Name":"王五","Height":177,"Sex":"男"}]}
[root@localhost gotest]#
这样看不是很直观,看下面这种形式:
{
"Name": "中关村大学",
"Area": 1024,
"Student": [
{
"Name": "张三",
"Height": 180,
"Sex": "男"
},
{
"Name": "王丽",
"Height": 165,
"Sex": "女"
}
],
"Teacher": [
{
"Name": "李四",
"Height": 180,
"Sex": "女"
},
{
"Name": "王五",
"Height": 177,
"Sex": "男"
}
]
}
二、解析:func Unmarshal
func Unmarshal(data []byte, v interface{}) error
作用:Unmarshal 解析 JSON 数据,并将结果存储在 v 中。如果 v 为 nil 或不是指针,则 Unmarshal 返回 InvalidUnmarshalError,例如:
package main
import (
"encoding/json"
"fmt"
)
type Students struct {
Name string `Json:"name"`
Height int `Json:"height"`
Sex string `Json:"sex"`
}
type Teachers struct {
Name string `Json:"name"`
Height int `Json:"height"`
Sex string `Json:"sex"`
}
type School struct {
Name string `Json:"name"`
Area int `Json:"area"`
Student []Students `Json:"student"`
Teacher []Teachers `Json:"teacher"`
}
func main() {
var school School
strJson := `{"name":"中关村大学","area":1024,"student":[{"name":"张三","height":180,"sex":"男"},{"name":"王丽","height":165,"sex":"女"}],"teacher":[{"name":"李四","height":180,"sex":"女"},{"name":"王五","height":177,"sex":"男"}]}`
err := json.Unmarshal([]byte(strJson), &school) //注意:第二个参数需要传递地址
if err != nil {
fmt.Println("Umarshal Error:" + err.Error())
return
}
fmt.Printf("%#v\n", school)
fmt.Println(school.Name, school.Area)
}
输出:
[root@localhost gotest]# go run main.go
main.School{Name:"中关村大学", Area:1024, Student:[]main.Students{main.Students{Name:"张三", Height:180, Sex:"男"}, main.Students{Name:"王丽", Height:165, Sex:"女"}}, Teacher:[]main.Teachers{main.Teachers{Name:"李四", Height:180, Sex:"女"}, main.Teachers{Name:"王五", Height:177, Sex:"男"}}}
中关村大学 1024
[root@localhost gotest]#
这里需要注意的是,结构体内的变量名称首字母要大写,这样其它的包才可以访问该结构的变量。可以通过在变量后面加 `Json:“变量名称”` 来解析Json中的对应的变量。
CSDN博客专家🏆,华为云享专家🏆,Linux、C/C++、面试、刷题、算法尽管咨询我,关注我,有问题私聊!
欢迎小伙伴们点赞👍、收藏⭐、留言💬
- 点赞
- 收藏
- 关注作者
评论(0)