Last active
June 10, 2022 13:59
-
-
Save phunguyen19/ed3ac78d686240f4aa7cedb4eeb5edef to your computer and use it in GitHub Desktop.
golang json to struct example
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 ( | |
| "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