Created
January 22, 2016 22:02
-
-
Save mwmahlberg/2a66e7190ce9d69b6064 to your computer and use it in GitHub Desktop.
Using json.Decoder to decode API call.
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" | |
"net/http" | |
) | |
// So ungefähr | |
type Contrib struct { | |
Id string `json:"userid"` | |
Name string `json:"user"` | |
Title string | |
} | |
type Query struct { | |
Contribs []Contrib `json:"usercontribs"` | |
} | |
type Response struct { | |
Query Query | |
} | |
const ( | |
url = "http://www.funtoo.org/api.php?action=query&list=usercontribs&format=json&ucuser=%s&ucdir=newer" | |
) | |
func main() { | |
res, err := http.Get(fmt.Sprintf(url,"jubalh")) | |
if err != nil { | |
panic(err) | |
} | |
r := Response{} | |
dec := json.NewDecoder(res.Body) | |
if err := dec.Decode(&r); err != nil { | |
panic(err) | |
} | |
fmt.Printf("%v\n", len(r.Query.Contribs)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment