Skip to content

Commit a15eba5

Browse files
new example
1 parent 4a17575 commit a15eba5

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

‎chapter7/listing31/listing31.go‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// This sample program demonstrates how to decode a JSON string.
2+
package main
3+
4+
import (
5+
"encoding/json"
6+
"fmt"
7+
"log"
8+
)
9+
10+
// JSON contains a sample string to unmarshal.
11+
var JSON = `{
12+
"name": "Gopher",
13+
"title": "programmer",
14+
"contact": {
15+
"home": "415.333.3333",
16+
"cell": "415.555.5555"
17+
}
18+
}`
19+
20+
func main() {
21+
// Unmarshal the JSON string into our map variable.
22+
var c map[string]interface{}
23+
err := json.Unmarshal([]byte(JSON), &c)
24+
if err != nil {
25+
log.Println("ERROR:", err)
26+
return
27+
}
28+
29+
data, err := json.MarshalIndent(c, "", " ")
30+
if err != nil {
31+
log.Println("ERROR:", err)
32+
return
33+
}
34+
35+
fmt.Println(string(data))
36+
}

0 commit comments

Comments
 (0)