Skip to content

Instantly share code, notes, and snippets.

@ikuwow
Last active November 1, 2015 23:52
Show Gist options
  • Save ikuwow/9f0749fe3653917d2319 to your computer and use it in GitHub Desktop.
Save ikuwow/9f0749fe3653917d2319 to your computer and use it in GitHub Desktop.
Go言語で簡単にHTTPリクエストを送ってJSONをパースするサンプル ref: http://qiita.com/ikuwow/items/c8f494bbd16adf6db142
package main
import (
"os"
"fmt"
"net/http"
"github.com/m0a/easyjson"
)
var api = "https://teratail.com/api/v1"
func run() error {
resp, err := http.Get(api+"/questions")
if err != nil {
return fmt.Errorf("Failed to connect teratail.com")
}
defer resp.Body.Close()
jsonData, err := easyjson.NewEasyJson(resp.Body)
if err != nil {
return fmt.Errorf("Invalid responses")
}
for _, v:=range jsonData.K("questions").RangeObjects() {
fmt.Printf("%s\n", v.K("title"))
}
return nil
}
func main() {
if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment