Skip to content

Instantly share code, notes, and snippets.

@dolanor
Last active Dec 17, 2015
Embed
What would you like to do?
Query the JCDecaux Vélib' API for a station Id
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"encoding/json"
"os"
)
func main() {
stationId := os.Args[1]
// You need to change that API KEY. Get it here: https://developer.jcdecaux.com/
const yourApiKey = "ae55d65e9bbe2bf740c7fbd3915d583d3aa"
res, err := http.Get("https://api.jcdecaux.com/vls/v1/stations/" + stationId + "?contract=Paris&apiKey=" + yourAp
iKey)
if err != nil {
log.Fatal(err)
}
stationsBytes, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
log.Fatal(err)
}
// We don't know the JSON "schema", let's get a generic way
var jsonstations interface{}
jsonerr := json.Unmarshal(stationsBytes, &jsonstations)
if jsonerr != nil {
log.Fatal(jsonerr)
}
m := jsonstations.(map[string]interface{})
for k, v := range m {
switch vv := v.(type) {
case string:
fmt.Println(k, "is string:", vv)
case int:
fmt.Println(k, "is int:", vv)
case float64:
fmt.Println(k, "is float64:", vv)
case []interface{}:
fmt.Println(k, "is an array:")
for i, u := range vv {
fmt.Println(i, u)
}
case map[string]interface{}:
fmt.Println(k, "is a map:")
for i, u := range vv {
fmt.Println(i, u)
}
case bool:
fmt.Println(k, "is a bool:", vv)
default:
fmt.Println(k, "is of a type I don't know how to handle")
}
}
}
@fclairamb
Copy link

fclairamb commented Jul 13, 2014

Un petit coup de "go fmt" ne ferait pas de mal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment