go中对json数据的解析和封装
2020-02-02 00:30
513 查看
说起对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}}]
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- OKhttp封装 Json数据解析
- Android学习--封装成json数据和解析json数据
- JAVA解析和封装JSON格式数据
- java解析json数据(前台json数据封装后台springmvc解析)
- 同步异步网络请求封装以及数据JSON解析
- java 封装解析 Json数据。
- cocos2d-x 3.0 (vs2012环境下亲测)把json数据转换成unicode编码来传送用户数据并且实现json数据的解析和封装
- [系列] Go 如何解析 JSON 数据?
- javascript应用:页面解析list和map封装后的json数据
- 【COCOS2D-X(1.X 2.X) Json(cpp版)及新加字体库篇】在Cocos2dx引擎中封装、解析Json(cpp版)数据以及添加自定义字体库
- 详解将jsoncpp编译so库,进行封装和解析JSON数据
- 项目实战:JSON数据封装与解析及常见问题(文末小彩蛋)
- json数据格式常用解析和封装方式
- java后台封装和解析json数据
- [系列] Go 如何解析 JSON 数据?
- cocos2d-x 3.0 (vs2012环境下亲测)把json数据转换成unicode编码来传送用户数据并且实现json数据的解析和封装
- 学习笔记 Tianmao 篇 使用简单封装后的自定义OkHttp 获取json被GSON解析后的数据
- iOS JSON数据封装与解析
- jQuery封装解析数据(数组,json对象)
- 泛型解析JSON数据封装