{"key1" : val1, "key2" : val2}
import (
"encoding/json"
"fmt"
"strconv"
)
type Peoson struct {
Name string `json:"name"`
Age int `json:"age"`
Birthday string `json:"birthday"`
Salary float64 `json:"salary"`
}
// 1. 结构体序列化
func testStructJSON() {
var person1 Peoson = Peoson{
Name: "tom",
Age: 12,
Birthday: "1998-12-8",
Salary: 65552.98,
}
// 序列化方法: func Marshal(v interface{}) ([]byte, error)
jsonStr, err := json.Marshal(person1)
if err != nil {
fmt.Printf("err: %v\n", err)
return
}
// jsonStr: {"name":"tom","age":12,"birthday":"1998-12-8","salary":65552.98}
fmt.Printf("jsonStr: %v\n", string(jsonStr))
}
// 2. map序列化
func testMapJson() {
var a map[string]int = make(map[string]int)
a["aaa"] = 11
a["bbb"] = 22
a["ccc"] = 33
a["ddd"] = 44
a["eee"] = 55
// 序列化
aStr, err := json.Marshal(a)
if err != nil {
fmt.Printf("err: %v\n", err)
return
}
// aStr: {"aaa":11,"bbb":22,"ccc":33,"ddd":44,"eee":55}
fmt.Printf("aStr: %v\n", string(aStr))
}
// 3. slice序列化
func testSliceJson() {
var slice []map[string]int = make([]map[string]int, 0)
var tempMap1 map[string]int = make(map[string]int)
tempMap1["aaa"] = 11
tempMap1["bbb"] = 22
tempMap1["ccc"] = 33
slice = append(slice, tempMap1)
var tempMap2 map[string]int = make(map[string]int)
tempMap2["tom"] = 21
tempMap2["jack"] = 22
tempMap2["mary"] = 23
slice = append(slice, tempMap2)
// 序列化
sliceStr, err := json.Marshal(slice)
if err != nil {
fmt.Printf("err: %v\n", err)
return
}
// sliceStr: [{"aaa":11,"bbb":22,"ccc":33},{"jack":22,"mary":23,"tom":21}]
fmt.Printf("sliceStr: %v\n", string(sliceStr))
}
// 4. 普通数据类型序列化
func testFloat64() {
var val float64 = 36.33366
// 序列化
valStr, err := json.Marshal(val)
if err != nil {
fmt.Printf("err: %v\n", err)
return
}
res, _ := strconv.ParseFloat(string(valStr), 10)
// valStr: 36.33366
fmt.Printf("valStr: %v\n", res)
}
func main() {
testStructJSON()
testMapJson()
testSliceJson()
testFloat64()
}
反序列化是指,将json字符串反序列化成对应的数据类型
import (
"encoding/json"
"fmt"
)
type Peoson struct {
Name string `json:"name"`
Age int `json:"age"`
Birthday string `json:"birthday"`
Salary float64 `json:"salary"`
}
// 1. 反序列为struct
func UnSerializeStruct() {
str := "{\"name\":\"tom\",\"age\":12,\"birthday\":\"1998-12-8\",\"salary\":65552.98}"
var person Peoson
// func Unmarshal(data []byte, v interface{}) error
err := json.Unmarshal([]byte(str), &person) // 引用传递
if err != nil {
fmt.Printf("err: %v\n", err)
return
}
// person: {tom 12 1998-12-8 65552.98}
fmt.Printf("person: %v\n", person)
}
// 2. 反序列化map
func UnmarshalMap() {
str := "{\"aaa\":11,\"bbb\":22,\"ccc\":33,\"ddd\":44,\"eee\":55}"
var amap map[string]int // 反序列不需要 make,底层会创建
// 反序列化
err := json.Unmarshal([]byte(str), &amap) // &引用不能省
if err != nil {
fmt.Printf("err: %v\n", err)
return
}
// amap: map[aaa:11 bbb:22 ccc:33 ddd:44 eee:55]
fmt.Printf("amap: %v\n", amap)
}
// 3. 反序列化slice
func UnmarshalSlice() {
str := "[{\"aaa\":11,\"bbb\":22,\"ccc\":33},{\"jack\":22,\"mary\":23,\"tom\":21}]"
var slice []map[string]int // 不需要 make
err := json.Unmarshal([]byte(str), &slice) // &引用不能省
if err != nil {
fmt.Printf("err: %v\n", err)
return
}
// slice: [map[aaa:11 bbb:22 ccc:33] map[jack:22 mary:23 tom:21]]
fmt.Printf("slice: %v\n", slice)
}
// 4. 反序列化基本数据类型
func UnmarshalFloat64() {
str := "36.363636"
var a float64
err := json.Unmarshal([]byte(str), &a)
if err != nil {
fmt.Printf("err: %v\n", err)
return
}
// a: 36.363636
fmt.Printf("a: %v\n", a)
}
func main() {
UnSerializeStruct()
UnmarshalMap()
UnmarshalSlice()
UnmarshalFloat64()
}