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
+10k Golang : Ordinal and Ordinalize a given number to the English ordinal numeral
+28.9k Golang : Detect (OS) Operating System
+11.7k Golang : Simple file scaning and remove virus example
+14k Golang : Get current time
+8.9k Golang : Random integer with rand.Seed() within a given range
+6.1k Fontello : How to load and use fonts?
+11.7k Golang : Surveillance with web camera and OpenCV
+21.9k SSL : How to check if current certificate is sha1 or sha2
+4.8k PHP : Extract part of a string starting from the middle
+31k Golang : Download file example
+24.2k Golang : Upload to S3 with official aws-sdk-go package
+21.2k Golang : Sort and reverse sort a slice of strings