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
+7.3k Golang : Of hash table and hash map
+15.3k nginx: [emerg] unknown directive "ssl"
+4.7k Unix/Linux : How to pipe/save output of a command to file?
+26.4k Golang : Get executable name behind process ID example
+17.4k Golang : Check if IP address is version 4 or 6
+4.8k PHP : Extract part of a string starting from the middle
+5.9k Golang : Use NLP to get sentences for each paragraph example
+5.9k Golang : Denco multiplexer example
+5.8k Unix/Linux : How to test user agents blocked successfully ?
+5.3k Golang : Get FX sentiment from website example
+21.6k Golang : Encrypt and decrypt data with TripleDES