Last active
January 3, 2018 03:37
-
-
Save eyeezzi/4f75adeebda0b4220e1641713321abda to your computer and use it in GitHub Desktop.
How to Read Generic JSON in Golang.
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" | |
| ) | |
| func main() { | |
| // Go stores JSON data as an array of bytes. | |
| jstr := `{"fname":"john","lname":"doe","age":25}` | |
| b := []byte(jstr) | |
| // In Go, a generic object has type of 'interface{}' | |
| // JSON is either an object of key-value pairs, i.e map[string]interface{} | |
| // ...or an array of objects, i.e []interface{} | |
| var jobj map[string]interface{} | |
| // Unmarshalling => decoding JSON to Go object. | |
| if err := json.Unmarshal(b, &jobj); err != nil { | |
| fmt.Println(err.Error()) | |
| } | |
| fmt.Println(jobj["fname"]) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment