标准库代码
func Marshal(v any) ([]byte, error) {
e := newEncodeState()
defer encodeStatePool.Put(e)
err := e.marshal(v, encOpts{escapeHTML: true})
if err != nil {
return nil, err
}
buf := append([]byte(nil), e.Bytes()...)
return buf, nil
}
func Unmarshal(data []byte, v any) error {
// Check for well-formedness.
// Avoids filling out half a data structure
// before discovering a JSON syntax error.
var d decodeState
err := checkValid(data, &d.scan)
if err != nil {
return err
}
d.init(data)
return d.unmarshal(v)
}
一、单个值序列化与反序列化
aaaa := 321
aa2, err := json.Marshal(aaaa)//序列化
if err != nil {
fmt.Printf("err: %v\n", err)
}
fmt.Printf("aa2: %v\n", aa2) //aa2: [51 50 49]
var a1 int
err = json.Unmarshal(aa2, &a1) //反序列化
fmt.Printf("a1: %v %[1]T\n", a1) //a1: 321 int
二、结构体
package main
import (
"encoding/json"
"fmt"
)
type Strcut1 struct {
Name string
Age int
Hight int
}
func main() {
//一、结构体序列化与反序列化
sperson := []Strcut1{ //person结构类型的切片
{"aa", 3, 54},
{"aa", 2, 54},
{"aa", 6, 54},
{"aa", 3, 52},
{"aa", 12, 15},
}
//序列化
sp1, err := json.Marshal(sperson)
if err != nil {
fmt.Printf("err: %v\n", err)
}
fmt.Printf("sp1: %T %[1]s %[1]v\n", sp1) //[]uint8 [{"Name":"aa","Age":3,"Hight":54},{"Name":"aa","Age":2,"Hight":54},{"Name":"aa","Age":6,"Hight":54},{"Name":"aa","Age":3,"Hight":52},{"Name":"aa","Age":12,"Hight":15}] [91 123 34 78 97 109 101 34 58 34 97 97 34 44 34 65 103 101 34 58 51 44 34 72 105 103 104 116 34 58 53 52 125 44 123 34 78 97 109 101 34 58 34 97 97 34 44 34 65 103 101 34 58 50 44 34 72 105 103 104 116 34 58 53 52 125 44 123 34 78 97 109 101 34 58 34 97 97 34 44 34 65 103 101 34 58 54 44 34 72 105 103 104 116 34 58 53 52 125 44 123 34 78 97 109 101 34 58 34 97 97 34 44 34 65 103 101 34 58 51 44 34 72 105 103 104 116 34 58 53 50 125 44 123 34 78 97 109 101 34 58 34 97 97 34 44 34 65 103 101 34 58 49 50 44 34 72 105 103 104 116 34 58 49 53 125 93]
//反序列化
var desp1 []Strcut1 //接收
err = json.Unmarshal(sp1, &desp1)
if err != nil {
fmt.Printf("err: %v\n", err)
}
fmt.Printf("desp1: %T %[1]v %[1]q\n", desp1) //[]main.Strcut1 [{aa 3 54} {aa 2 54} {aa 6 54} {aa 3 52} {aa 12 15}] [{"aa" '\x03' '6'} {"aa" '\x02' '6'} {"aa" '\x06' '6'} {"aa" '\x03' '4'} {"aa" '\f' '\x0f'}]
}
任意类型切片
package main
import (
"encoding/json"
"fmt"
)
func main() {
//二、任意类型切片序列化与反序列化
slis := []any{
"sss",
[]int{1, 2, 3, 4, 5},
666,
map[int]int{1: 2, 3: 4},
}
//整体序列化
slisall, err := json.Marshal(slis)
if err != nil {
fmt.Printf("err: %v \n", err)
}
fmt.Printf("slisall: %v %[1]T\n", slisall) //slisall: [91 34 115 115 115 34 44 91 49 44 50 44 51 44 52 44 53 93 44 54 54 54 44 123 34 49 34 58 50 44 34 51 34 58 52 125 93] []uint8
//整体反序列化
var aa []any //slis 定义的类型接收
err = json.Unmarshal(slisall, &aa)
fmt.Printf("aa: %v %[1]T\n", aa) //aa: [sss [1 2 3 4 5] 666 map[1:2 3:4]] []interface {}
//每个子值序列化的集合
var sli1 [][]byte //返回的是[]uint8,所以这里接收要写[][]uint8 或[][]uint8
for _, v := range slis {
s, err := json.Marshal(v)
if err != nil {
fmt.Printf("err: %v\n", err)
}
sli1 = append(sli1, s)
fmt.Printf("s: %v\n", s)
}
fmt.Printf("sli1: %T %[1]s %[1]v\n", sli1) //sli1: [][]uint8 ["sss" [1,2,3,4,5] 666 {"1":2,"3":4}] [[34 115 115 115 34] [91 49 44 50 44 51 44 52 44 53 93] [54 54 54] [123 34 49 34 58 50 44 34 51 34 58 52 125]]
//每个子值序列化的集合 反序列化
var aas []any
for _, v := range sli1 {
var aas1 any
err := json.Unmarshal(v, &aas1)
if err != nil {
fmt.Printf("err: %v\n", err)
}
aas = append(aas, aas1)
}
fmt.Printf("aas: %T %[1]v\n", aas) //aas: []interface {} [sss [1 2 3 4 5] 666 map[1:2 3:4]]
}