Go: 一个 JSON 切分成两个对象

Jan 30 2019 Golang

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package main

import (
"encoding/json"
"fmt"
)

// User is
type User struct {
Email string `json:"email"`
Password string `json:"password"`
}

// Blog is
type Blog struct {
Title string `json:"title"`
Content string `json:"content"`
}

func main() {

b := new(Blog)
u := new(User)

json.Unmarshal([]byte(
`{
"email":"lishude",
"password":"test",
"title": "hello",
"content": "content"
}`),
&struct {
*User
*Blog
}{u, b})

fmt.Println(b)
fmt.Println(u)
}