Skip to content

Instantly share code, notes, and snippets.

@michiel
Last active December 17, 2015 22:59
Show Gist options
  • Save michiel/5686201 to your computer and use it in GitHub Desktop.
Save michiel/5686201 to your computer and use it in GitHub Desktop.
JSON unmarshal in go
package main
import (
"fmt"
"encoding/json"
"os"
"io/ioutil"
)
/*
{
"records" : [
{
"id" : 2341213123,
"person" : {
"name" : "John Doe"
}
},
{
"id" : 2578424523,
"person" : {
"name" : "Jane Doe"
}
}
]
}
*/
type jsonobject struct {
Records []RecordsType
}
type RecordsType struct {
Id int
Person PersonType
}
type PersonType struct {
Name string
}
func main() {
file, e := ioutil.ReadFile("./ex.json")
if e != nil {
fmt.Printf("Read/parse error: %v\n", e)
os.Exit(1)
}
var jsondata jsonobject
json.Unmarshal(file, &jsondata)
for i := range jsondata.Records {
fmt.Printf("Name: %v\n", jsondata.Records[i].Person.Name)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment