Golang encoding/json.Decoder.Decode() function example
package encoding/json
Decode reads the next JSON-encoded value from its input and stores it in the value pointed to by v (1st parameter ).
Golang encoding/json.Decoder.Decode() 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 { // <-- here
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
+11k Android Studio : Highlight ImageButton when pressed on example
+25.5k Golang : How to check if a connection to database is still alive ?
+7.7k Golang : Accept any number of function arguments with three dots(...)
+9.5k Golang : Create matrix with Gonum Matrix package example
+10.7k Golang : md5 hash of a string
+10.8k Golang : Print UTF-8 fonts on image example
+9.7k Golang : Create Temporary File
+15.2k Golang : Fix cannot convert buffer (type *bytes.Buffer) to type string error
+9.4k Golang : Flip coin example
+9.6k Golang : Get local time and equivalent time in different time zone
+6.5k Golang : Handling Yes No Quit query input
+8.4k Golang : Convert(cast) string to int64