Skip to content

Commit 7951241

Browse files
authored
Added json.NewDecoder example (#566)
* Update json.go adding example for json.NewDecoder * updated as per Contributings.md instructions
1 parent bc680ed commit 7951241

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

examples/json/json.go

+8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"encoding/json"
99
"fmt"
1010
"os"
11+
"strings"
1112
)
1213

1314
// We'll use these two structs to demonstrate encoding and
@@ -121,4 +122,11 @@ func main() {
121122
enc := json.NewEncoder(os.Stdout)
122123
d := map[string]int{"apple": 5, "lettuce": 7}
123124
enc.Encode(d)
125+
126+
// We can also decode JSON directly from `os.Reader`
127+
dec := json.NewDecoder(strings.NewReader(str))
128+
res1 := response2{}
129+
dec.Decode(&res1)
130+
fmt.Println(res1)
131+
fmt.Println(res1.Fruits[1])
124132
}

examples/json/json.hash

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
35295476f817fe575619b6168273a29eddd7f545
2-
JOQpRGJWAxR
1+
75a5664321dafe10fa623c187b90f079c5319a3c
2+
jR7IEUPQF8T

public/json

+20-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)