Skip to content

Instantly share code, notes, and snippets.

@kunalkushwaha
Last active April 5, 2018 22:17
Show Gist options
  • Save kunalkushwaha/ce226b8e134e8c40c675 to your computer and use it in GitHub Desktop.
Save kunalkushwaha/ce226b8e134e8c40c675 to your computer and use it in GitHub Desktop.
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
)
/*
sample json
{
"Humans":{
"People":[
{
"Name":"John",
"City":"Rome",
"Pin":675675
}
],
"Animal":[
{
"Name":"Julie",
"Type":"Dog",
"Age":5,
"Owner":"John"
}
]
}
}
*/
type jsonData struct {
Humans `json:"Humans"`
}
type Humans struct {
PeopleData []People `json:"People"`
AnimalData []Animal `json:"Animal"`
}
type People struct {
Name string `json:"Name"`
City string `json:"City"`
Pin int `json:"Pin"`
}
type Animal struct {
Name string `json:"Name"`
Type string `json:"Type"`
Age int `json:"Age"`
Owner string `json:"Owner"`
}
func main() {
file, err := ioutil.ReadFile("./test.json")
if err != nil {
fmt.Println("Error while opening file")
return
}
people := make([]People, 0)
animal := make([]Animal, 0)
data := jsonData{Humans{people,animal}}
err = json.Unmarshal(file, &data)
if err != nil {
fmt.Println("Error while parsing file")
return
}
fmt.Println(data)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment