Skip to content

Instantly share code, notes, and snippets.

@uurtech
Created May 28, 2020 14:50
Show Gist options
  • Save uurtech/a8705ce0e4effc6ba35e5eef30b9c7b9 to your computer and use it in GitHub Desktop.
Save uurtech/a8705ce0e4effc6ba35e5eef30b9c7b9 to your computer and use it in GitHub Desktop.
Golang Json Parse with Struct
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