-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.go
50 lines (43 loc) · 772 Bytes
/
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
package main
import (
"fmt"
"encoding/json"
)
const jsonStr = `
{
"array": [
1,
2,
3
],
"boolean": true,
"null": null,
"number": 123,
"object": {
"a": "b",
"c": "d",
"e": "f"
},
"string": "Hello World"
}
`
type partT struct {
O map[string]string `json:"object,omitempty"`
S string `json:"string,omitempty"`
}
func main() {
blob := []byte(jsonStr)
// decode
var whole interface{}
json.Unmarshal(blob, &whole)
fmt.Printf("whole => %#+v\n", whole)
var part partT
json.Unmarshal(blob, &part)
fmt.Printf("part => %#+v\n", part)
fmt.Println()
// encode
blob, _ = json.Marshal(whole)
fmt.Printf("whole => %s\n", string(blob))
blob, _ = json.MarshalIndent(part, "", " ")
fmt.Printf("part => %s\n", string(blob))
}