Last active
December 17, 2015 22:59
-
-
Save michiel/5686201 to your computer and use it in GitHub Desktop.
JSON unmarshal in go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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