forked from ardanlabs/gotraining
/
example1.go
72 lines (61 loc) · 1.89 KB
/
example1.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
//
// go build
// ./example1
//
// Sample program to show how to unmarshal JSON data from an API.
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
)
// citiBikeURL provides the station statuses of CitiBike bike sharing stations.
const citiBikeURL = "https://gbfs.citibikenyc.com/gbfs/en/station_status.json"
// stationData is used to unmarshal the JSON document returned form citiBikeURL.
type stationData struct {
LastUpdated int `json:"last_updated"`
TTL int `json:"ttl"`
Data struct {
Stations []station `json:"stations"`
} `json:"data"`
}
// station is used to unmarshal each of the station documents in stationData.
type station struct {
ID string `json:"station_id"`
NumBikesAvailable int `json:"num_bikes_available"`
NumBikesDisabled int `json:"num_bike_disabled"`
NumDocksAvailable int `json:"num_docks_available"`
NumDocksDisabled int `json:"num_docks_disabled"`
IsInstalled int `json:"is_installed"`
IsRenting int `json:"is_renting"`
IsReturning int `json:"is_returning"`
LastReported int `json:"last_reported"`
HasAvailableKeys bool `json:"eightd_has_available_keys"`
}
func main() {
// Get the JSON response from the URL.
response, err := http.Get(citiBikeURL)
if err != nil {
log.Fatal(err)
}
// Defer closing the response body.
defer response.Body.Close()
// Read the body of the response into []byte.
body, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatal(err)
}
// Declare a variable of type stationData.
var sd stationData
// Unmarshal the JSON data into the variable.
if err := json.Unmarshal(body, &sd); err != nil {
log.Fatal(err)
return
}
// Print the first station.
fmt.Printf("%+v\n\n", sd.Data.Stations[0])
}