Go-JSon
JSon
json数据在线解析
https://www.json.cn/
结构体 map 切片 序列化
结构体序列化
package main
import (
"encoding/json"
"fmt"
)
type Monster struct {
Name string
Age int
Birthday string
Sal float64
Skill string
}
func testStruct() {
monster := Monster{
Name: "牛魔王",
Age: 500,
Birthday: "2011-11-11",
Sal: 8000.0,
Skill: "牛魔拳",
}
//序列化
data, err := json.Marshal(&monster)
if err != nil {
fmt.Printf("序列化失败err=%v", err)
}
fmt.Printf("monster序列化后=%v", string(data))
}
func main() {
testStruct()
}
map序列化
package main
import (
"encoding/json"
"fmt"
)
type Monster struct {
Name string
Age int
Birthday string
Sal float64
Skill string
}
func testStruct() {
monster := Monster{
Name: "牛魔王",
Age: 500,
Birthday: "2011-11-11",
Sal: 8000.0,
Skill: "牛魔拳",
}
//序列化
data, err := json.Marshal(&monster)
if err != nil {
fmt.Printf("序列化失败err=%v", err)
}
fmt.Printf("monster序列化后=%v", string(data))
}
func testMap() {
//var a map[string]interface{}
a := make(map[string]interface{})
a["name"] = "红名海尔"
a["age"] = 38
a["address"] = "小好几年"
data, err := json.Marshal(a)
//map类型本身就是指针类型
if err != nil {
fmt.Printf("序列化失败err=%v", err)
}
fmt.Printf("a map 序列化后=%v", string(data))
}
func main() {
testStruct()
fmt.Println()
testMap()
}
切片序列化
package main
import (
"encoding/json"
"fmt"
)
type Monster struct {
Name string
Age int
Birthday string
Sal float64
Skill string
}
func testStruct() {
monster := Monster{
Name: "牛魔王",
Age: 500,
Birthday: "2011-11-11",
Sal: 8000.0,
Skill: "牛魔拳",
}
//序列化
data, err := json.Marshal(&monster)
if err != nil {
fmt.Printf("序列化失败err=%v", err)
}
fmt.Printf("monster序列化后=%v", string(data))
}
func testMap() {
//var a map[string]interface{}
a := make(map[string]interface{})
a["name"] = "红名海尔"
a["age"] = 38
a["address"] = "小好几年"
data, err := json.Marshal(a)
//map类型本身就是指针类型
if err != nil {
fmt.Printf("序列化失败err=%v", err)
}
fmt.Printf("a map 序列化后=%v", string(data))
}
func testSlice() {
var slice []map[string]interface{}
m1 := make(map[string]interface{})
m1["name"] = "谢谢"
m1["age"] = 90
m1["address"] = "叔叔是"
slice = append(slice, m1)
m2 := make(map[string]interface{})
m2["name"] = "水水"
m2["age"] = 75
m2["address"] = "是我"
slice = append(slice, m2)
data, err := json.Marshal(slice)
//map类型本身就是指针类型
if err != nil {
fmt.Printf("序列化失败err=%v", err)
}
fmt.Printf("slice序列化后=%v", string(data))
}
func main() {
testStruct()
fmt.Println()
testMap()
fmt.Println()
testSlice()
}
基本数据类型序列化
package main
import (
"encoding/json"
"fmt"
)
type Monster struct {
Name string
Age int
Birthday string
Sal float64
Skill string
}
func testStruct() {
monster := Monster{
Name: "牛魔王",
Age: 500,
Birthday: "2011-11-11",
Sal: 8000.0,
Skill: "牛魔拳",
}
//序列化
data, err := json.Marshal(&monster)
if err != nil {
fmt.Printf("序列化失败err=%v", err)
}
fmt.Printf("monster序列化后=%v", string(data))
}
func testMap() {
//var a map[string]interface{}
a := make(map[string]interface{})
a["name"] = "红名海尔"
a["age"] = 38
a["address"] = "小好几年"
data, err := json.Marshal(a)
//map类型本身就是指针类型
if err != nil {
fmt.Printf("序列化失败err=%v", err)
}
fmt.Printf("a map 序列化后=%v", string(data))
}
func testSlice() {
var slice []map[string]interface{}
m1 := make(map[string]interface{})
m1["name"] = "谢谢"
m1["age"] = 90
m1["address"] = "叔叔是"
slice = append(slice, m1)
m2 := make(map[string]interface{})
m2["name"] = "水水"
m2["age"] = 75
m2["address"] = "是我"
slice = append(slice, m2)
data, err := json.Marshal(slice)
//map类型本身就是指针类型
if err != nil {
fmt.Printf("序列化失败err=%v", err)
}
fmt.Printf("slice序列化后=%v", string(data))
}
func testFloat64() {
num1 := 2345.67
data, err := json.Marshal(&num1)
//map类型本身就是指针类型
if err != nil {
fmt.Printf("序列化失败err=%v", err)
}
fmt.Printf("slice序列化后=%v", string(data))
}
func main() {
testStruct()
fmt.Println()
testMap()
fmt.Println()
testSlice()
fmt.Println()
testFloat64()
}
也可以在map里面放数组
package main
import (
"encoding/json"
"fmt"
)
func testMap() {
//var a map[string]interface{}
a := make(map[string]interface{})
a["name"] = "红名海尔"
a["age"] = 38
a["address"] = [2]string{"模型", "好好"}
data, err := json.Marshal(a)
//map类型本身就是指针类型
if err != nil {
fmt.Printf("序列化失败err=%v", err)
}
fmt.Printf("a map 序列化后=%v", string(data))
}
func main() {
testMap()
fmt.Println()
}
转载请注明出处,欢迎讨论和交流!