内容简介:对于json的序列化和反序列化,go的encoding/json 包提供了一些列的方法。 常用的比如
序列化和反序列化
1、JSON的序列化
1.1序列化 struct、map、slice
对于json的序列化和反序列化,go的encoding/json 包提供了一些列的方法。 常用的比如
func Marshal(v interface{}) ([]byte, error)
序列化
func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error)
同Marshal,并且可以格式化
第一个参数是要序列化的数据,第二个是每行的前缀,第三个是每行的缩进,比如MarshalIndent(map,""," ")
json-iterator ,有 java 和 go 两种实现
序列化练习:
package main
import (
"encoding/json"
"fmt"
)
type Student struct {
Name string
Age int
Height float32
isMarried bool
}
func main(){
testStruct()
testMap()
testSlice()
}
func testStruct(){
student:=Student{
Name:"张三",
Age:18,
Height:178.12,
isMarried:false,
}
bytes, err := json.Marshal(student)
if err!=nil{
fmt.Println("序列化错误")
}
fmt.Printf("序列化struct %v\n",string(bytes))
}
func testMap(){
m:=make(map[string]interface{})
m["result"]="1"
m["message"]="success"
m["data"]=Student{"孙悟空",10000,160.50,false}
if bytes, e := json.Marshal(m);e==nil{
fmt.Printf("序列化map %v\n",string(bytes))
}else{
fmt.Println("序列化错误")
}
}
func testSlice(){
m:=make(map[string]interface{})
s:=[]Student{Student{"孙悟空",10000,160.50,false},Student{"猪八戒",10000,180.50,true}}
m["result"]="1"
m["message"]="success"
m["data"]=s
if bytes, e := json.Marshal(m);e==nil{
fmt.Printf("序列化slice %v\n",string(bytes))
}else{
fmt.Println("序列化错误")
}
}
//结果
序列化struct {"Name":"张三","Age":18,"Height":178.12}
序列化map {"data":{"Name":"孙悟空","Age":10000,"Height":160.5},"message":"success","result":"1"}
序列化slice {"data":[{"Name":"孙悟空","Age":10000,"Height":160.5},{"Name":"猪八戒","Age":10000,"Height":180.5}],"message":"success","result":"1"}
1.2 自定义序列化后json key
json包通过反射机制序列化,我们可以通过添加struct 的tag,自定义返回json 的key
只需要添加一个 json:"key"
这种方式就可以
package main
import (
"encoding/json"
"fmt"
)
type Student struct {
Name string `json:"newName"` //tag标签、反射机制
Age int `json:"newAge"`
Height float32
isMarried bool
}
func main(){
testSlice()
}
func testSlice(){
m:=make(map[string]interface{})
s:=[]Student{Student{"孙悟空",10000,160.50,false},Student{"猪八戒",10000,180.50,true}}
m["result"]="1"
m["message"]="success"
m["data"]=s
if bytes, e := json.Marshal(m);e==nil{
fmt.Printf("序列化slice %v\n",string(bytes))
}else{
fmt.Println("序列化错误")
}
}
//序列化slice {"data":[{"newName":"孙悟空","newAge":10000,"Height":160.5},{"newName":"猪八戒","newAge":10000,"Height":180.5}],"message":"success","result":"1"}
2 json反序列化
func Unmarshal(data []byte, v interface{}) error
反序列化,接收一个byte数组和要解析的类型,返回error,如果解析成功返回nil。使用时注意,无需实例化,Unmarshal内部实现了,比如map ,struct .
//后两个配合使用,比如接收网络返回结果:err = json.NewDecoder(resp.Body).Decode(&gr)
func NewDecoder(r io.Reader) *Decoder
func (dec *Decoder) Decode(v interface{}) error
用这个函数就够了,看代码
package main
import (
"encoding/json"
"fmt"
)
/*
序列化struct {"Name":"张三","Age":18,"Height":178.12}
序列化map {"data":{"Name":"孙悟空","Age":10000,"Height":160.5},"message":"success","result":"1"}
序列化slice {"data":[{"Name":"孙悟空","Age":10000,"Height":160.5},{"Name":"猪八戒","Age":10000,"Height":180.5}],"message":"success","result":"1"}
*/
type Student struct {
Name string
Age int
Height float32
isMarried bool
}
func main() {
testStruct()
testMap()
testSlice()
}
func testStruct() {
strStruct := "{\"Name\":\"张三\",\"Age\":18,\"Height\":178.12}"
var std Student //无需实例化,Unmarshal内部实现了
err := json.Unmarshal([]byte(strStruct), &std)
if err != nil {
fmt.Println("testStruct反序列化失败",err)
}
fmt.Printf("反序列化json结果:%+v\n", std)
}
func testMap(){
str:="{\"data\":{\"Name\":\"孙悟空\",\"Age\":10000,\"Height\":160.5},\"message\":\"success\",\"result\":\"1\"}"
var m map[string]interface{}
err := json.Unmarshal([]byte(str), &m)
if err != nil {
fmt.Println("testMap反序列化失败",err)
}
fmt.Printf("反序列化json结果:%v\n", m)
}
func testSlice(){
str:="[{\"Name\":\"孙悟空\",\"Age\":10000,\"Height\":160.5},{\"Name\":\"猪八戒\",\"Age\":10000,\"Height\":180.5}]"
var sli []Student
err := json.Unmarshal([]byte(str), &sli)
if err != nil {
fmt.Println("testSlice反序列化失败",err)
}
fmt.Printf("反序列化json结果:%+v\n", sli)
}
3、第三方库
| 1 | encoding/json | Golang原生 | |
|---|---|---|---|
| 2 | easyjson | https://github.com/mailru/eas... | |
| 4 | iterator/json | https://github.com/json-itera... |
除了我们上面的原生标准库的 encoding/json ,我推荐使用下面两款第三方库,性能都很高。
iterator对动态支持更好。具体使用看官方文档。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Windows高级调试
Mario Hewardt、Daniel Pravat / 聂雪军 / 机械工业出版社 / 2009-5 / 79.00元
本书主要讲解Windows高级调试思想和工具,并涉及一些高级调试主题。本书内容主要包括:工具简介、调试器简介、调试器揭密、符号文件与源文件的管理、栈内存破坏、堆内存破坏、安全、进程间通信、资源泄漏、同步、编写定制的调试扩展、64位调试、事后调试、Windows Vista基础以及应用程序验证器的测试设置等。本书内容详实、条理清楚。 本书适合Windows开发人员、Windows测试人员和Windo......一起来看看 《Windows高级调试》 这本书的介绍吧!