type User struct {
Name string
FansCount int64
}
// 如果反序列化的时候指定明确的结构体和变量类型
func TestJsonUnmarshal(t *testing.T) {
const jsonStream = `
{"name":"ethancai", "fansCount": 9223372036854775807}
`
var user User // 类型为User
err := JsonUnmarshal(jsonStream, &user)
if err != nil {
fmt.Println("error:", err)
}
fmt.Printf("%+v \n", user)
}
type Person struct {
Name string
Age int
}
type Family struct {
Persons []Person
}
// 解析多维数组
var f Family
// 模拟传输的Json数据
familyJSON := `{"Persons": [{"Name":"Elinx","Age":26}, {"Name":"Twinkle","Age":21}] }`
fmt.Println("======================")
// 解析字符串为Json
json.Unmarshal([]byte(familyJSON), &f)
运行结果
=== RUN TestJsonMash
======================
{[{Elinx 26 0001-01-01 00:00:00 +0000 UTC []} {Twinkle 21 0001-01-01 00:00:00 +0000 UTC []}]}
{"Persons":[{"Name":"Elinx","Age":26,"Birth":"0001-01-01T00:00:00Z","Children":null}
--- PASS: TestJsonMash (0.00s)
PASS
struct能被转换的字段都是首字母大写的字段,但如果想要在json中使用小写字母开头的key,可以使用struct的tag来辅助反射。
type Post struct {
Id int `json:"ID"`
Content string `json:"content"`
Author string `json:"author"`
Label []string `json:"label"`
}
func TestJsonMash1(t *testing.T){
postp := &Post{
2,
"Hello World",
"userB",
[]string{"linux", "shell"},
}
p, _ := json.MarshalIndent(postp, "", "\t")
fmt.Println(string(p))
}
运行结果:
=== RUN TestJsonMash1
{
"ID": 2,
"content": "Hello World",
"author": "userB",
"label": [
"linux",
"shell"
]
}
--- PASS: TestJsonMash1 (0.00s)
PASS
json:"-"
来表示本字段不转换为json数据,即使这个字段名首字母大写json:"-,"
,也就是加上一个逗号,omitempty
选项,那么如果这个字段的值为0值,即false、0、""、nil等,这个字段将不会转换到json中,string
选项,那么这个字段的值将转换成json字符串{
"id": 1,
"content": "hello world",
"author": {
"id": 2,
"name": "userA"
},
"published": true,
"label": [],
"nextPost": null,
"comments": [{
"id": 3,
"content": "good post1",
"author": "userB"
},
{
"id": 4,
"content": "good post2",
"author": "userC"
}
]
}
测试代码
type Post struct {
ID int64 `json:"id"`
Content string `json:"content"`
Author Author `json:"author"`
Published bool `json:"published"`
Label []string `json:"label"`
NextPost *Post `json:"nextPost"`
Comments []*Comment `json:"comments"`
}
type Author struct {
ID int64 `json:"id"`
Name string `json:"name"`
}
type Comment struct {
ID int64 `json:"id"`
Content string `json:"content"`
Author string `json:"author"`
}
func TestJsonStruct(t *testing.T){
jsonData :="{\n \"id\": 1,\n \"content\": \"hello world\",\n \"author\": {\n \"id\": 2,\n \"name\": \"userA\"\n },\n \"published\": true,\n \"label\": [],\n \"nextPost\": null,\n \"comments\": [{\n \"id\": 3,\n \"content\": \"good post1\",\n \"author\": \"userB\"\n },\n {\n \"id\": 4,\n \"content\": \"good post2\",\n \"author\": \"userC\"\n }\n ]\n}"
var post Post
// 解析json数据到post中
err := json.Unmarshal([]byte(jsonData), &post)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(post)
fmt.Println(post.Author, post.Content, post.Comments[0].Content,post.Comments[0].ID, post.Comments[0].Author )
}
运行结果
=== RUN TestJsonStruct
{1 hello world {2 userA} true [] <nil> [0xc00016d1a0 0xc00016d1d0]}
{2 userA} hello world good post1 3 userB
--- PASS: TestJsonStruct (0.00s)
PASS