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()
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"}
Go