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.2k Golang : Print how to use flag for your application example
+6.9k Golang : Decode XML data from RSS feed
+26.9k Golang : Find files by extension
+5.7k PHP : Convert string to timestamp or datestamp before storing to database(MariaDB/MySQL)
+31.6k Golang : bufio.NewReader.ReadLine to read file line by line
+4.4k Linux/MacOSX : Search and delete files by extension
+28k Golang : Decode/unmarshal unknown JSON data type with map[string]interface
+7.5k Golang : Hue, Saturation and Value(HSV) with OpenCV example
+32.1k Golang : Convert an image file to []byte
+21.9k Golang : Convert string slice to struct and access with reflect example
+11.6k Golang : Change date format to yyyy-mm-dd