Golang encoding/json.Unmarshal() function example
package encoding/json
Unmarshal parses the JSON-encoded data (1st parameter) and stores the result in the value pointed to by v (2nd parameter).
Golang encoding/json.Unmarshal() function usage example
package main
import (
"encoding/json"
"fmt"
)
func main() {
var jsondata = []byte(`[
{"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
}
var workers []Employee
err := json.Unmarshal(jsondata, &workers)
if err != nil {
fmt.Println(err)
}
fmt.Printf("%+v \n", workers)
}
Output :
[{Name:Adam Age:36 Job:CEO} {Name:Eve Age:34 Job:CFO} {Name:Mike Age:38 Job:COO}]
Reference :
Advertisement
Something interesting
Tutorials
+10.6k Golang : How to detect a server/machine network interface capabilities?
+9.3k Golang : How to delete element(data) from map ?
+13.4k Golang : Get URI segments by number and assign as variable example
+35.1k Golang : Convert date or time stamp from string to time.Time type
+9.9k Golang : Read until certain character to break for loop
+25k Golang : Convert file content into array of bytes
+12.4k Golang : Activate web camera and broadcast out base64 encoded images
+11.3k Golang : Get absolute path to binary for os.Exec function with exec.LookPath
+5.3k Golang : Extract sub-strings
+13.1k Golang : GUI with Qt and OpenCV to capture image from camera
+16.4k Golang : How to log each HTTP request to your web server?