Go语言基础(十四)—— Go语言切片,map,结构体与Json的序列化,反序列化

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/The_Reader/article/details/84490897

序列化和反序列化

序列化概念理解:就是将变量从内存中变成可存储或传输的过程称之为序列化,序列化之后,就可以把序列化后的内容写入磁盘,或者通过网络传输到别的机器上。

反序列化则就是序列化反过来,把变量内容从序列化的对象重新读到内存里称之为反序列化

序列化结构体案例:

package main

import (
	"encoding/json"
	"fmt"
)

//属性必须大写,不然json解析不出来
type Person struct {
	Name string
	Age int
	Sex string
	Hobby []string
}

var wek,alice Person
func init() {
	//初始化两个结构体
	wek = Person{"wek",18,"男",[]string{"alice","play","code"}}
	alice = Person{"alice",17,"女",[]string{"wek","study","teacher"}}
}

func main() {
	//序列化wek结构体
	wekBytes, e := json.Marshal(wek)
	if e!=nil{
		fmt.Println("结构体序列化失败!")
	}
	//打印wek序列化结构体
	fmt.Println("序列化结果为:",wekBytes)
	fmt.Println("序列化结果转化为string后为:",string(wekBytes))
	//序列化alice结构体
	aliceBytes, e := json.Marshal(alice)
	if e!=nil{
		fmt.Println("结构体序列化失败!")
	}
	//打印alice序列化结果
	fmt.Println("序列化结果为:",aliceBytes)
	fmt.Println("序列化结果转化为string后为:",string(aliceBytes))
}

结果为:

序列化结果为: [123 34 78 97 109 101 34 58 34 119 101 107 34 44 34 65 103 101 34 58 49 56 44 34 83 101 120 34 58 34 231 148 183 34 44 34 72 111 98 98 121 34 58 91 34 97 108 105 99 101 34 44 34 112 108 97 121 34 44 34 99 111 100 101 34 93 125]
序列化结果转化为string后为: {"Name":"wek","Age":18,"Sex":"男","Hobby":["alice","play","code"]}
序列化结果为: [123 34 78 97 109 101 34 58 34 97 108 105 99 101 34 44 34 65 103 101 34 58 49 55 44 34 83 101 120 34 58 34 229 165 179 34 44 34 72 111 98 98 121 34 58 91 34 119 101 107 34 44 34 115 116 117 100 121 34 44 34 116 101 97 99 104 101 114 34 93 125]
序列化结果转化为string后为: {"Name":"alice","Age":17,"Sex":"女","Hobby":["wek","study","teacher"]}

也可以将序列化结果转化为string后使用json在线解析工具查看:

序列化map案例:

package main

import (
	"encoding/json"
	"fmt"
)

//属性必须大写,不然json解析不出来
type Person struct {
	Name string
	Age int
	Sex string
	Hobby []string
}

//var wek,alice Person
var wek1= make(map[string]interface{})
func init() {
	/*//初始化两个结构体
	wek = Person{"wek",18,"男",[]string{"alice","play","code"}}
	alice = Person{"alice",17,"女",[]string{"wek","study","teacher"}}
	*/
	wek1["Name"]="wek1"
	wek1["Age"]=18
	wek1["Sex"]="男"
	wek1["hobby"]=[]string{"alice","play","code"}


}

func main() {
	/*//序列化wek结构体
	wekBytes, e := json.Marshal(wek)
	if e!=nil{
		fmt.Println("结构体序列化失败!")
	}
	//打印wek序列化结构体
	fmt.Println("序列化结果为:",wekBytes)
	fmt.Println("序列化结果转化为string后为:",string(wekBytes))
	//序列化alice结构体
	aliceBytes, e := json.Marshal(alice)
	if e!=nil{
		fmt.Println("结构体序列化失败!")
	}
	//打印alice序列化结果
	fmt.Println("序列化结果为:",aliceBytes)
	fmt.Println("序列化结果转化为string后为:",string(aliceBytes))

	fmt.Println("-------------------------------------")*/

	wek1Bytes, e:= json.Marshal(wek1)
	if e!=nil{
		fmt.Println("map序列化失败!")
	}
	fmt.Println(wek1Bytes)
	fmt.Println(string(wek1Bytes))

}

结果为:

序列化切片:

package main

import (
	"encoding/json"
	"fmt"
)

//属性必须大写,不然json解析不出来
type Person struct {
	Name string
	Age int
	Sex string
	Hobby []string
}

var wek,alice Person
//var wek1= make(map[string]interface{})
var person []Person
func init() {
	//初始化两个结构体
	wek = Person{"wek",18,"男",[]string{"alice","play","code"}}
	alice = Person{"alice",17,"女",[]string{"wek","study","teacher"}}
	person= append(person, wek)
	person= append(person, alice)

	/*	wek1["Name"]="wek1"
		wek1["Age"]=18
		wek1["Sex"]="男"
		wek1["hobby"]=[]string{"alice","play","code"}
	*/

}

func main() {
	/*//序列化wek结构体
	wekBytes, e := json.Marshal(wek)
	if e!=nil{
		fmt.Println("结构体序列化失败!")
	}
	//打印wek序列化结构体
	fmt.Println("序列化结果为:",wekBytes)
	fmt.Println("序列化结果转化为string后为:",string(wekBytes))
	//序列化alice结构体
	aliceBytes, e := json.Marshal(alice)
	if e!=nil{
		fmt.Println("结构体序列化失败!")
	}
	//打印alice序列化结果
	fmt.Println("序列化结果为:",aliceBytes)
	fmt.Println("序列化结果转化为string后为:",string(aliceBytes))

	fmt.Println("-------------------------------------")

	wek1Bytes, e:= json.Marshal(wek1)
	if e!=nil{
		fmt.Println("map序列化失败!")
	}
	fmt.Println(wek1Bytes)
	fmt.Println(string(wek1Bytes))*/

	personBytes, e := json.Marshal(person)
	if e!=nil{
		fmt.Println("切片序列化失败!")
	}
	fmt.Println(personBytes)
	fmt.Println(string(personBytes))

	

}

结果为:

 

猜你喜欢

转载自blog.csdn.net/The_Reader/article/details/84490897