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 :

http://golang.org/pkg/encoding/json/#Unmarshal

Advertisement