go中对json数据的解析和封装

说起对json数据的操作,无非就两种,封装和解析
一.解析

  1. 已知的数据格式,直接解析到结构体
type person struct {
	Name string `json:"name"`
	Age int    `json:"age"`
	School school `json:"school"`
}
type school struct {
	Grade int `json:"grade"`
	Class int `json:"class"`
}
func main(){
    var p person
    str:=`{"name":"xxx","age":12,"school":{"grade":9,"class":1}}`
    if eir:=json.Unmarshal([]byte(str),&p);eir!=nil{
		fmt.Println(eir)
	}else{
		fmt.Println(p)
	}

}
输出:
{xxx 12 {9 1}}
  1. 未知的数据结构,解析到map
type person struct {
	Name string `json:"name"`
	Age int    `json:"age"`
	School school `json:"school"`
}
type school struct {
	Grade int `json:"grade"`
	Class int `json:"class"`
}
func main(){
	da:=make(map[string]interface{})
    str:=`{"name":"xxx","age":12,"school":{"grade":9,"class":1}}`
    if eir:=json.Unmarshal([]byte(str),&da);eir!=nil{
		fmt.Println(eir)
	}else{
		for key,value :=range da{
			fmt.Println(key,value)
		}
	}

}
输出:
name xxx
age 12
school map[class:1 grade:9]

3.未知的数据结构,利用simplejson解析

type person struct {
	Name   string `json:"name"`
	Age    int    `json:"age"`
	School school `json:"school"`
}
type school struct {
	Grade int `json:"grade"`
	Class int `json:"class"`
}

func main() {
	str := `{"name":"xxx","age":12,"school":{"grade":9,"class":1}}`
	if js, eir:= simplejson.NewJson([]byte(str));eir!=nil{
		fmt.Println(eir)
	}else{
		var student person
		if _, ok := js.CheckGet("name");ok{
			name:=js.Get("name").MustString()
			student.Name=name
		}
		if _, ok := js.CheckGet("age");ok{
			age:=js.Get("age").MustInt()
			student.Age=age
		}
		if _, ok := js.CheckGet("school");ok{
			class:=js.Get("school").Get("class").MustInt()
			grade:=js.Get("school").Get("grade").MustInt()
			student.School.Class=class
			student.School.Grade=grade
		}
		fmt.Println(student)
	}
}
结果:
{xxx 12 {9 1}}

来个稍微复杂一点的(数组)

type person struct {
	Name   string `json:"name"`
	Age    int    `json:"age"`
	School school `json:"school"`
}
type school struct {
	Grade int `json:"grade"`
	Class int `json:"class"`
}

func main() {
	str := `{"data":[{"name":"xxx","age":12,"school":{"grade":9,"class":1}},{"name":"xxx","age":12,"school":{"grade":9,"class":1}}]}`
	if js, eir := simplejson.NewJson([]byte(str)); eir != nil {
		fmt.Println(eir)
	} else {
		stus, _ := js.Get("data").Array()
		for i, _ := range stus {
			person := js.Get("data").GetIndex(i)
			name := person.Get("name").MustString()
			age := person.Get("age").MustInt()
			class := person.Get("school").Get("class").MustInt()
			grade := person.Get("school").Get("grade").MustInt()
			fmt.Printf("name=%s,age=%d,grade=%d,class=%d\n", name, age, grade, class)

		}
	}
}
结果:
name=xxx,age=12,grade=9,class=1
name=xxx,age=12,grade=9,class=1

4.未知的数据结构,利用gjson解析

type person struct {
	Name   string `json:"name"`
	Age    int    `json:"age"`
	School school `json:"school"`
}
type school struct {
	Grade int `json:"grade"`
	Class int `json:"class"`
}

func main() {
	var stus []person
	var stu person
	str := `{"data":[{"name":"xxx","age":12,"school":{"grade":9,"class":1}},{"name":"xxx","age":12,"school":{"grade":9,"class":1}}]}`
	if !gjson.Valid(str) {
		fmt.Println("json is not Valid ")
	}
	for _, v := range gjson.Get(str, `data`).Array() {
		stu.Age=int(gjson.Get(v.String(), `age`).Int())
		stu.Name=gjson.Get(v.String(), `name`).String()
		stu.School.Grade=int(gjson.Get(v.String(), `school.grade`).Int())
		stu.School.Class=int(gjson.Get(v.String(), `school.class`).Int())
		stus = append(stus, stu)
	}
	fmt.Println(stus)
}
结果:
[{xxx 12 {9 1}} {xxx 12 {9 1}}]

二.封装

type person struct {
	Name   string `json:"name"`
	Age    int    `json:"age"`
	School school `json:"school"`
}
type school struct {
	Grade int `json:"grade"`
	Class int `json:"class"`
}

func main() {
	var stus []person
	var stu person
	stu.Name="xxx"
	stu.Age=12
	stu.School.Class=1
	stu.School.Grade=9
	stus = append(stus, stu)
	stu.Name="xxx"
	stu.Age=12
	stu.School.Class=1
	stu.School.Grade=9
	stus = append(stus, stu)
	json,_:=json.Marshal(stus)
	fmt.Println(string(json))
}
结果:
[{"name":"xxx","age":12,"school":{"grade":9,"class":1}},{"name":"xxx","age":12,"school":{"grade":9,"class":1}}]

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值