说起对json数据的操作,无非就两种,封装和解析
一.解析
- 已知的数据格式,直接解析到结构体
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}}
- 未知的数据结构,解析到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}}]