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
+7.6k Javascript : Push notifications to browser with Push.js
+41.2k Golang : How to count duplicate items in slice/array?
+11.1k Golang : Roll the dice example
+9.1k Golang : Simple histogram example
+14.4k Android Studio : Use image as AlertDialog title with custom layout example
+17.4k Golang : Multi threading or run two processes or more example
+9.1k Golang : Serving HTTP and Websocket from different ports in a program example
+18.5k Golang : Aligning strings to right, left and center with fill example
+17.7k How to enable MariaDB/MySQL logs ?
+6.9k Golang : Calculate BMI and risk category
+6.2k Golang : Get Hokkien(福建话)/Min-nan(閩南語) Pronounciations
+19.9k Golang : Count JSON objects and convert to slice/array