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
+17k Golang : Fix cannot convert buffer (type *bytes.Buffer) to type string error
+4.7k MariaDB/MySQL : Form select statement or search query with Chinese characters
+13.4k Golang : error parsing regexp: invalid or unsupported Perl syntax
+11.8k Golang : convert(cast) float to string
+7.6k Golang : Convert(cast) io.Reader type to string
+8.1k Golang : Variadic function arguments sanity check example
+12k Golang : Decompress zlib file example
+19.1k Mac OSX : Homebrew and Golang
+9.4k Golang : Create unique title slugs example
+22.1k Golang : Join arrays or slices example
+14k Golang : Compress and decompress file with compress/flate example
+4.7k Fix Google Analytics Redundant Hostnames problem