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
|
package main
import (
"fmt"
"encoding/json"
)
// Field is a key value structure
type Field struct {
name string
value string
}
func main() {
fields := []Field{}
jsonStr := `[
{"a": "alles klar", "b": "c", "d": "1"},
{"key": "value"}
]`
data := []map[string]interface{}{}
// Convert json string to list of map[string]interface{}
err := json.Unmarshal([]byte(jsonStr), &data)
if err != nil {
panic(err)
}
for _, v := range data {
// Keys contains available keys in current map
keys := make([]string, 0, len(v))
// Copy keys
for k := range v {
keys = append(keys, k)
}
for _, key := range keys {
field := Field {
name: key,
value: fmt.Sprintf("%s", v[key].(string)),
}
fields = append(fields, field)
}
}
for _, f := range fields {
fmt.Printf("%#v\n", f)
}
}
|