diff --git a/examples/json/json.go b/examples/json/json.go index 259478632..aac870e77 100644 --- a/examples/json/json.go +++ b/examples/json/json.go @@ -8,6 +8,7 @@ import ( "encoding/json" "fmt" "os" + "strings" ) // We'll use these two structs to demonstrate encoding and @@ -121,4 +122,11 @@ func main() { enc := json.NewEncoder(os.Stdout) d := map[string]int{"apple": 5, "lettuce": 7} enc.Encode(d) + + // We can also decode JSON directly from `os.Reader` + dec := json.NewDecoder(strings.NewReader(str)) + res1 := response2{} + dec.Decode(&res1) + fmt.Println(res1) + fmt.Println(res1.Fruits[1]) } diff --git a/examples/json/json.hash b/examples/json/json.hash index 60f6c01bd..6958f8dad 100644 --- a/examples/json/json.hash +++ b/examples/json/json.hash @@ -1,2 +1,2 @@ -35295476f817fe575619b6168273a29eddd7f545 -JOQpRGJWAxR +75a5664321dafe10fa623c187b90f079c5319a3c +jR7IEUPQF8T diff --git a/public/json b/public/json index b5d0f5815..55f2280ce 100644 --- a/public/json +++ b/public/json @@ -46,7 +46,7 @@ data types.
package main
os.Writer
s like
os.Stdout
or even HTTP response bodies.
- enc := json.NewEncoder(os.Stdout)
d := map[string]int{"apple": 5, "lettuce": 7}
- enc.Encode(d)
+ enc.Encode(d)
+ We can also decode JSON directly from os.Reader
dec := json.NewDecoder(strings.NewReader(str))
+ res1 := response2{}
+ dec.Decode(&res1)
+ fmt.Println(res1)
+ fmt.Println(res1.Fruits[1])
}