Created
May 28, 2020 14:50
-
-
Save uurtech/a8705ce0e4effc6ba35e5eef30b9c7b9 to your computer and use it in GitHub Desktop.
Golang Json Parse with Struct
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" | |
"io/ioutil" | |
"log" | |
"net/http" | |
) | |
type Data []struct { | |
UserID string `json:"userId"` | |
ID string `json:"id"` | |
Title string `json:"title"` | |
Body string `json:"body"` | |
} | |
func main() { | |
resp, err := http.Get("https://jsonplaceholder.typicode.com/posts/") | |
if err != nil { | |
log.Fatal(err) | |
} | |
body, _ := ioutil.ReadAll(resp.Body) | |
var f Data | |
err = json.Unmarshal(body, &f) | |
fmt.Printf("%+v", f[0].Body) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment