Golang encoding/json.NewDecoder() function example
package encoding/json
NewDecoder returns a new decoder that reads from r (1st parameter). The decoder introduces its own buffering and may read data from r beyond the JSON values requested.
Golang encoding/json.NewDecoder() function usage example
package main
import (
"encoding/json"
"fmt"
"strings"
"io"
)
func main() {
var jsonDataStream = `
{"Name":"Adam","Age":36,"Job":"CEO"}
{"Name":"Eve","Age":34,"Job":"CFO"}
{"Name":"Mike","Age":38,"Job":"COO"}
`
type Employee struct {
Name string
Age int
Job string
}
decoder := json.NewDecoder(strings.NewReader(jsonDataStream))
for {
var worker Employee
if err := decoder.Decode(&worker); err == io.EOF {
break
} else if err != nil {
fmt.Println(err)
}
fmt.Printf("%s | %d | %s\n", worker.Name, worker.Age, worker.Job)
}
}
Output :
Adam | 36 | CEO
Eve | 34 | CFO
Mike | 38 | COO
Reference :
Advertisement
Something interesting
Tutorials
+17.6k Golang : Parse date string and convert to dd-mm-yyyy format
+21.8k SSL : How to check if current certificate is sha1 or sha2
+13k Golang : Get terminal width and height example
+19.9k Golang : Accept input from user with fmt.Scanf skipped white spaces and how to fix it
+9.8k Golang : Resumable upload to Google Drive(RESTful) example
+9.8k Golang : Qt get screen resolution and display on center example
+11.4k Golang : Concatenate (combine) buffer data example
+6.3k PHP : How to handle URI or URL with non-ASCII characters such as Chinese/Japanese/Korean(CJK) ?
+35.9k Golang : Integer is between a range
+16.9k Golang : Get the IPv4 and IPv6 addresses for a specific network interface
+7.6k SSL : How to check if current certificate is sha1 or sha2 from command line
+8.8k Golang : On lambda, anonymous, inline functions and function literals