-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathjson-list.go
165 lines (145 loc) · 3.46 KB
/
json-list.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Using go to unmarshal json lists with multiple types
// http://mattyjwilliams.blogspot.co.uk/2013/01/using-go-to-unmarshal-json-lists-with.html
package main
import (
"encoding/json"
"fmt"
)
var jsonStr = []byte(`
{
"things": [
{
"name": "Alice",
"age": 37
},
{
"city": "Ipoh",
"country": "Malaysia"
},
{
"name": "Bob",
"age": 36
},
{
"city": "Northampton",
"country": "England"
}
]
}`)
func main() {
personsA, placesA := solutionA(jsonStr)
fmt.Printf("%d %d\n", len(personsA), len(placesA))
personsB, placesB := solutionB(jsonStr)
fmt.Printf("%d %d\n", len(personsB), len(placesB))
personsC, placesC := solutionC(jsonStr)
fmt.Printf("%d %d\n", len(personsC), len(placesC))
}
// Common to both solutions
type Person struct {
Name string
Age int
}
type Place struct {
City string
Country string
}
//Solution A
//Unmarshal into a map
//Type assert when we need it
func solutionA(jsonStr []byte) ([]Person, []Place) {
persons := []Person{}
places := []Place{}
var data map[string][]map[string]interface{} // {'key' => [{'key'=> },{'key'=> }]} 这样的结构
err := json.Unmarshal(jsonStr, &data)
if err != nil {
fmt.Println(err)
return persons, places
}
for i := range data["things"] {
item := data["thing"][i]
if item["name"] != nil {
persons = addPerson(persons, item)
} else {
places = addPlace(places, item)
}
}
return persons, places
}
func addPerson(persons []Person, item map[string]interface{}) []Person {
name, _ := item["name"].(string)
age, _ := item["age"].(int)
person := Person{name, age}
persons = append(persons, person)
return persons
}
func addPlace(places []Place, item map[string]interface{}) []Place {
city, _ := item["city"].(string)
country, _ := item["city"].(string)
place := Place{city, country}
places = append(places, place)
return places
}
// SolutionB
type Mixed struct {
Name string `json:"name"`
Age int `json:"age"`
City string `json:"city"`
Country string `json:"country"`
}
func solutionB(jsonStr []byte) ([]Person, []Place) {
persons := []Person{}
places := []Place{}
var data map[string][]Mixed
err := json.Unmarshal(jsonStr, &data)
if err != nil {
fmt.Println(err)
return persons, places
}
for i := range data["thing"] {
item := data["things"][i]
if item.Name != "" {
persons = append(persons, Person{item.Name, item.Age})
} else {
places = append(places, Place{item.City, item.Country})
}
}
return persons, places
}
//SolutionC
func solutionC(jsonStr []byte) ([]Person, []Place) {
people := []Person{}
places := []Place{}
var data map[string][]json.RawMessage
err := json.Unmarshal(jsonStr, &data)
if err != nil {
fmt.Println(err)
return people, places
}
for _, thing := range data["thing"] {
people = addPersonC(thing, people)
places = addPlaceC(thing, places)
}
return people, places
}
func addPersonC(thing json.RawMessage, people []Person) []Person {
person := Person{}
if err := json.Unmarshal(thing, &person); err != nil {
fmt.Println(err)
} else {
if person != *new(Person) {
people = append(people, person)
}
}
return people
}
func addPlaceC(thing json.RawMessage, places []Place) []Place {
place := Place{}
if err := json.Unmarshal(thing, &place); err != nil {
fmt.Println(err)
} else {
if place != *new(Place) {
places = append(places, place)
}
}
return places
}