Created
January 27, 2019 03:28
-
-
Save melvinodsa/463eb7c518e90eb2dd93c5a861d35500 to your computer and use it in GitHub Desktop.
Parse JSON in Golang
This file contains 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" | |
"log" | |
"strings" | |
) | |
func main() { | |
jsonStr := `{ | |
"ID": "Book1", | |
"Name": "Harry Potter and the Deathly Hallows", | |
"Author": "J. K. Rowling" | |
}` | |
decoder := json.NewDecoder(strings.NewReader(jsonStr)) | |
obj := struct { | |
ID string | |
Name string | |
Author string | |
}{} | |
err := decoder.Decode(&obj) | |
if err != nil { | |
log.Fatal("Error while parsing the json string", err) | |
} | |
fmt.Println("Parsed obj", obj) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment