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.1k CodeIgniter : Load different view for mobile devices
+14.5k Golang : How to shuffle elements in array or slice?
+5.5k Gogland : Datasource explorer
+16.1k Golang : ROT47 (Caesar cipher by 47 characters) example
+9.6k Golang : Convert(cast) string to int64
+26.2k Mac/Linux and Golang : Fix bind: address already in use error
+7.2k Golang : Get environment variable
+7.4k Golang : How to iterate a slice without using for loop?
+10.2k Golang : Compare files modify date example
+36.7k Golang : Save image to PNG, JPEG or GIF format.
+8.3k Golang : How To Use Panic and Recover