-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonunmarshal.go
57 lines (43 loc) · 910 Bytes
/
jsonunmarshal.go
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package main
import(
"os"
"fmt"
"encoding/json"
)
type Person struct {
Name Name
Email [] Email
}
type Name struct {
Family string
Personal string
}
type Email struct {
Kind string
Address string
}
//绑定到Person对象
func (p Person) String() String {
s := p.Name.Personal + " " + p.Name.Family
for _, v := range p.Email {
s += "\n" + v.Kind + ": " + v.Address
}
return s
}
func main() {
var person Person
loadJSON("person.json", &person)
fmt.Println("Person", person.String())
}
func loadJSON(fileName string, key interface{}) {
inFile, err := os.Open(fileName)
checkError(err)
decoder := json.NewDecoder(inFile)
checkError(err)
inFile.Close()
}
func checkError(err error) {
if err != nil {
fmt.Println(os.Stderr, "Fatal error: %s", err.Error())
os.Exit(1)
}