Skip to content

Commit 4a17575

Browse files
saving changes
1 parent 8f52a38 commit 4a17575

4 files changed

Lines changed: 138 additions & 1 deletion

File tree

‎chapter7/listing23/listing23.go‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// This sample program demonstrates how to decode a JSON response
2+
// using the json package and NewDecoder function.
3+
package main
4+
5+
import (
6+
"encoding/json"
7+
"fmt"
8+
"log"
9+
"net/http"
10+
)
11+
12+
type (
13+
// gResult maps to the result document received from the search.
14+
gResult struct {
15+
GsearchResultClass string `json:"GsearchResultClass"`
16+
UnescapedURL string `json:"unescapedUrl"`
17+
URL string `json:"url"`
18+
VisibleURL string `json:"visibleUrl"`
19+
CacheURL string `json:"cacheUrl"`
20+
Title string `json:"title"`
21+
TitleNoFormatting string `json:"titleNoFormatting"`
22+
Content string `json:"content"`
23+
}
24+
25+
// gResponse contains the top level document.
26+
gResponse struct {
27+
ResponseData struct {
28+
Results []gResult `json:"results"`
29+
} `json:"responseData"`
30+
}
31+
)
32+
33+
func main() {
34+
uri := "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&rsz=8&q=golang"
35+
36+
// Issue the search against Google.
37+
resp, err := http.Get(uri)
38+
if err != nil {
39+
log.Println("ERROR:", err)
40+
return
41+
}
42+
defer resp.Body.Close()
43+
44+
// Decode the JSON response into our struct type.
45+
var gr gResponse
46+
err = json.NewDecoder(resp.Body).Decode(&gr)
47+
if err != nil {
48+
log.Println("ERROR:", err)
49+
return
50+
}
51+
52+
fmt.Println(gr)
53+
54+
// Marshal the struct type into a pretty print
55+
// version of the JSON document.
56+
pretty, err := json.MarshalIndent(gr, "", " ")
57+
if err != nil {
58+
log.Println("ERROR:", err)
59+
return
60+
}
61+
62+
fmt.Println(string(pretty))
63+
}

‎chapter7/listing27/listing27.go‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
// Contact represents our JSON string.
11+
type Contact struct {
12+
Name string `json:"name"`
13+
Title string `json:"title"`
14+
Contact struct {
15+
Home string `json:"home"`
16+
Cell string `json:"cell"`
17+
} `json:"contact"`
18+
}
19+
20+
// JSON contains a sample string to unmarshal.
21+
var JSON = `{
22+
"name": "Gopher",
23+
"title": "programmer",
24+
"contact": {
25+
"home": "415.333.3333",
26+
"cell": "415.555.5555"
27+
}
28+
}`
29+
30+
func main() {
31+
// Unmarshal the JSON string into our variable.
32+
var c Contact
33+
err := json.Unmarshal([]byte(JSON), &c)
34+
if err != nil {
35+
log.Println("ERROR:", err)
36+
return
37+
}
38+
39+
fmt.Println(c)
40+
}

‎chapter7/listing29/listing29.go‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
fmt.Println("Name:", c["name"])
30+
fmt.Println("Title:", c["title"])
31+
fmt.Println("Contact")
32+
fmt.Println("H:", c["contact"].(map[string]interface{})["home"])
33+
fmt.Println("C:", c["contact"].(map[string]interface{})["cell"])
34+
}

‎chapter7/listing43/listing43.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func main() {
1818
return
1919
}
2020

21-
// Create a file to store the response.
21+
// Create a file to persist the response.
2222
file, err := os.Create(os.Args[2])
2323
if err != nil {
2424
fmt.Println(err)

0 commit comments

Comments
 (0)