我正在尝试执行 API 请求以从 steams 公共 API 获取一些信息(这主要是为了学习 Go 并且只是学习如何处理 Json / API 请求)到目前为止我已经得到了这段代码:package mainimport ( "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]GetAppNewsfunc (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)}事情似乎有效,好吧,我想但它没有按照我期望的方式对其进行格式化。它打印出来是这样的:map[appnews:{{0 []}}]您可以单击此处查看 JSON 响应的确切格式,如果有人能告诉我我的结构做错了什么,最后我希望能够像这样:fmt.Println(blah["appnews"]["appid"])它会返回440。这就是我真正要讲的,如果您需要更多具体信息,请告诉我!谢谢您的帮助
添加回答
举报
0/150
提交
取消