-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjson.go
65 lines (53 loc) · 1.06 KB
/
json.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
58
59
60
61
62
63
64
65
package main
import (
"encoding/json"
"fmt"
"time"
)
type Color int
type Personal struct {
Number int `json:"ReferenceNumber"`
*City
Company string
DateTime time.Time
color Color
}
type City struct {
CityNumber string
CityName string
Address string
PostalCode int
}
const (
Red Color = iota
Green
Blue
Yellow
White
Black
)
func (person *Personal) ChangeColor(c Color) {
person.color = c
}
func (person *Personal) ToJson() ([]byte, error) {
b, err := json.Marshal(person)
return b, err
}
func main() {
var personJson = []byte(
`{"ReferenceNumber":12345,
"CityNumber":"67890",
"CityName":"Kasukabe",
"Address":"central",
"PostalCode":1234567,
"Company":"hogehoge",
"DateTime":"2014-10-29T17:23:00Z",
"Color":2}`)
var person Personal
err := json.Unmarshal(personJson, &person)
if err != nil {
fmt.Println("error:", err)
}
fmt.Printf("%+v\n", person)
fmt.Printf("%+v\n", person.City)
}