22Go 语言——序列化和反序列化

序列化和反序列化

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、第三方库

1encoding/jsonGolang原生
2easyjsonhttps://github.com/mailru/easyjson
4iterator/jsonhttps://github.com/json-iterator/go

除了我们上面的原生标准库的 encoding/json ,我推荐使用下面两款第三方库,性能都很高。

iterator对动态支持更好。具体使用看官方文档。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值