Skip to content

Instantly share code, notes, and snippets.

@aisrael
Last active June 7, 2016 10:17
Show Gist options
  • Save aisrael/05153b9672978a4a7ceb4a7431b0a82b to your computer and use it in GitHub Desktop.
Save aisrael/05153b9672978a4a7ceb4a7431b0a82b to your computer and use it in GitHub Desktop.
type ApiResponse struct {
RawItems []json.RawMessage `json:"items"`
HasMore bool `json:"has_more"`
QuotaMax int `json:"quota_max"`
QuotaRemaining int `json:"quota_remaining"`
}
type Owner struct {
// ...
}
type Question struct {
// ...
}
func main() {
// perform HTTP request
decoder := json.NewDecoder(resp.Body)
var apiResp = new(ApiResponse)
err = decoder.Decode(&apiResp)
if err != nil {
log.Fatal(err)
os.Exit(1)
}
fmt.Printf("Has more? %v\n", apiResp.HasMore)
fmt.Printf("Quota max: %d\n", apiResp.QuotaMax)
fmt.Printf("Quota remaining: %d\n", apiResp.QuotaRemaining)
fmt.Printf("Unmarshaling %d items...\n", len(apiResp.RawItems))
for _, item := range apiResp.RawItems {
var question = new(Question)
err = json.Unmarshal(item, &question)
fmt.Printf("Question id: %d\n", question.QuestionId)
fmt.Printf("Question owner: %s\n", question.Owner.DisplayName)
fmt.Printf("Question title: %s\n", question.Title)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment