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
+20.2k Golang : Compare floating-point numbers
+51.1k Golang : Disable security check for HTTPS(SSL) with bad or expired certificate
+21.6k Golang : Encrypt and decrypt data with TripleDES
+9.7k Golang : Load ASN1 encoded DSA public key PEM file example
+7.5k Golang : Dealing with struct's private part
+6.4k Golang : How to search a list of records or data structures
+12.3k Golang : 2 dimensional array example
+12.6k Golang : Get absolute path to binary for os.Exec function with exec.LookPath
+11.7k Golang : Find age or leap age from date of birth example
+20.7k Golang : Read directory content with os.Open
+8.3k Swift : Convert (cast) Character to Integer?