go json序列化与反序列化
package main
import (
"encoding/json"
"fmt"
)
type Student struct{
Id string
Name string
Sex string
}
type Class struct{
Count int
Name string
Students []*Student
}
var rowJson = `{
"Count":0,
"Name":"101",
"Students":[
{"Id":"0","Name":"stu0","Sex":"man"},
{"Id":"1","Name":"stu1","Sex":"man"},
{"Id":"2","Name":"stu2","Sex":"man"},
{"Id":"3","Name":"stu3","Sex":"man"},
{"Id":"4","Name":"stu4","Sex":"man"},
{"Id":"5","Name":"stu5","Sex":"man"},
{"Id":"6","Name":"stu6","Sex":"man"},
{"Id":"7","Name":"stu7","Sex":"man"},
{"Id":"8","Name":"stu8","Sex":"man"},
{"Id":"9","Name":"stu9","Sex":"man"}
]
}`
func main() {
c := &Class{
Name :"101",
Count :0,
}
for i := 0; i < 10; i++ {
stu:= &Student{
Name: fmt.Sprintf("stu%d",i),
Sex: "man",
Id: fmt.Sprintf("%d",i),
}
c.Students = append(c.Students,stu)
}
data,err := json.Marshal(c) // 结构体序列化为字符串
fmt.Printf("你好啊 json marshal %s \n >>> %T ----- \n",data,data)
if err != nil {
fmt.Println("json marshal failed")
return
}
fmt.Printf("这是json:%s\n",string(data))
fmt.Printf("这是[]uint8:%s\n",data)
// json 反序列化
fmt.Println("unmarshalresultis\n\n")
var c1 *Class= &Class{}
err = json.Unmarshal([]uint8(rowJson),c1)
fmt.Printf("+++++++++++++++++++:%s\n",err)
if err != nil{
fmt.Println("unmarhsal failed")
return
}
fmt.Printf("c1:%#v\n",c1)
for _,v := range c1.Students{
fmt.Printf("stu:%#v\n",v)
}
}
序列化和反序列化:
你好啊 json marshal {"Count":0,"Name":"101","Students":[{"Id":"0","Name":"stu0","Sex":"man"},{"Id":"1","Name":"stu1","Sex":"man"},{"Id":"2","Name":"stu2","Sex":"man"},{"Id":"3","Name":"stu3","Sex":"man"},{"Id":"4","Name":"stu4",
"Sex":"man"},{"Id":"5","Name":"stu5","Sex":"man"},{"Id":"6","Name":"stu6","Sex":"man"},{"Id":"7","Name":"stu7","Sex":"man"},{"Id":"8","Name":"stu8","Sex":"man"},{"Id":"9","Name":"stu9","Sex":"man"}]}
>>> []uint8 -----
这是json:{"Count":0,"Name":"101","Students":[{"Id":"0","Name":"stu0","Sex":"man"},{"Id":"1","Name":"stu1","Sex":"man"},{"Id":"2","Name":"stu2","Sex":"man"},{"Id":"3","Name":"stu3","Sex":"man"},{"Id":"4","Name":"stu4","Sex":"man"
},{"Id":"5","Name":"stu5","Sex":"man"},{"Id":"6","Name":"stu6","Sex":"man"},{"Id":"7","Name":"stu7","Sex":"man"},{"Id":"8","Name":"stu8","Sex":"man"},{"Id":"9","Name":"stu9","Sex":"man"}]}
这是[]uint8:{"Count":0,"Name":"101","Students":[{"Id":"0","Name":"stu0","Sex":"man"},{"Id":"1","Name":"stu1","Sex":"man"},{"Id":"2","Name":"stu2","Sex":"man"},{"Id":"3","Name":"stu3","Sex":"man"},{"Id":"4","Name":"stu4","Sex":"m
an"},{"Id":"5","Name":"stu5","Sex":"man"},{"Id":"6","Name":"stu6","Sex":"man"},{"Id":"7","Name":"stu7","Sex":"man"},{"Id":"8","Name":"stu8","Sex":"man"},{"Id":"9","Name":"stu9","Sex":"man"}]}
unmarshalresultis
+++++++++++++++++++:%!s(<nil>)
c1:&main.Class{Count:0, Name:"101", Students:[]*main.Student{(*main.Student)(0xc0000b8840), (*main.Student)(0xc0000b8870), (*main.Student)(0xc0000b88a0), (*main.Student)(0xc0000b88d0), (*main.Student)(0xc0000b8930), (*main.Stude
nt)(0xc0000b8960), (*main.Student)(0xc0000b8990), (*main.Student)(0xc0000b89c0), (*main.Student)(0xc0000b89f0), (*main.Student)(0xc0000b8a20)}}
stu:&main.Student{Id:"0", Name:"stu0", Sex:"man"}
stu:&main.Student{Id:"1", Name:"stu1", Sex:"man"}
stu:&main.Student{Id:"2", Name:"stu2", Sex:"man"}
stu:&main.Student{Id:"3", Name:"stu3", Sex:"man"}
stu:&main.Student{Id:"4", Name:"stu4", Sex:"man"}
stu:&main.Student{Id:"5", Name:"stu5", Sex:"man"}
stu:&main.Student{Id:"6", Name:"stu6", Sex:"man"}
stu:&main.Student{Id:"7", Name:"stu7", Sex:"man"}
stu:&main.Student{Id:"8", Name:"stu8", Sex:"man"}
stu:&main.Student{Id:"9", Name:"stu9", Sex:"man"}
#对不是本类型的,序列化为他类型的默认值,而不是强制类型转换
package main
import (
"encoding/json"
"fmt"
)
type Student struct {
Name string `json:"name"`
Age int `json:"age"`
Score string
}
func testStruct() {
stu := &Student{
Name: "lili",
Age: 11,
Score: "11.11",
}
data, err := json.Marshal(stu)
if err != nil {
fmt.Printf("json.marshal failed, err:", err)
return
}
fmt.Println(string(data))
var stu1 Student
// 第二个参数记得传地址
json.Unmarshal(data, &stu1)
fmt.Printf("00 > stu1=%#v\n",stu1)
data1 := `{"name":223.23,"age":1122,"Score": "11.11"}`
var stu2 Student
json.Unmarshal([]byte(data1), &stu2)
fmt.Printf("01 > stu2=%#v\n",stu2)
}
func main() {
testStruct()
}
输出:
{"name":"lili","age":11,"Score":"11.11"}
00 > stu1=main.Student{Name:"lili", Age:11, Score:"11.11"}
01 > stu2=main.Student{Name:"", Age:1122, Score:"11.11"}
写入自己的博客中才能记得长久