json - With Go (golang) how can I Unmarshal data into a struct, and then call specific fields from the struct? -


i'm trying api request information steams public api (this learning go , learning how deal json / api requests) have gotten code far:

package main  import (     "encoding/json"     "fmt"     "io/ioutil"     "net/http"     "strconv" )  type steamapi struct {     apikey string }  type getappnews struct {     appnews struct {         appid     int `json:"appid"`         newsitems []struct {             gid           int    `json:"gid"`             title         string `json:"title"`             url           string `json:"url"`             isexternalurl bool   `json:"is_external_url"`             author        string `json:"author"`             contents      string `json:"contents"`             feedlabel     string `json:"feedlabel"`             date          int    `json:"date"`         } `json:"newsitems"`     } `json:"appnews"` }  type jsonresponse map[string]getappnews  func (s steamapi) getnewsforapp(appid, count, maxlength int) error {     sappid := strconv.itoa(appid)     scount := strconv.itoa(count)     smaxlength := strconv.itoa(maxlength)      resp, err := http.get("http://api.steampowered.com/isteamnews/getnewsforapp/v0002/?appid=" + sappid + "&count=" + scount + "&maxlength=" + smaxlength + "&format=json")     if err != nil {         return err     }      defer resp.body.close()      body, err := ioutil.readall(resp.body)     if err != nil {         return err     }      var jsonreturn jsonresponse      json.unmarshal(body, &jsonreturn)      fmt.println(jsonreturn)      return nil  }  func main() {         tester := steamapi{""}          tester.getnewsforapp(440, 3, 300) } 

things seem work, alright, guess not formatting way expect unmarshal. prints out this:

map[appnews:{{0 []}}]

you can click here see format of json response looks like, if tell me have done wrong struct, in end expect able go like:

fmt.println(blah["appnews"]["appid"]) , return 440.

thats got go off of, if need anymore specific information let me know! help!

the data fits struct fine, no need map[string]getappnews.

type jsonresponse map[string]getappnews should getappnews.

playground


Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -