Skip to content

Instantly share code, notes, and snippets.

@phunguyen19
Last active June 10, 2022 13:59
Show Gist options
  • Select an option

  • Save phunguyen19/ed3ac78d686240f4aa7cedb4eeb5edef to your computer and use it in GitHub Desktop.

Select an option

Save phunguyen19/ed3ac78d686240f4aa7cedb4eeb5edef to your computer and use it in GitHub Desktop.
golang json to struct example
package main
import (
"encoding/json"
"fmt"
)
type People struct {
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Age int `json:"age"`
ClassName string `json:"class_name"`
}
func main() {
sourceJSON := `[
{"first_name": "Victor", "last_name": "Nguyen", "age": 100, "class_name":"golang"},
{"first_name": "Anh", "last_name": "Dinh", "age":200, "class_name":"golang"}
]`
people := []People{}
err := json.Unmarshal([]byte(sourceJSON), &people)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(people)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment