I am building an application that reads information from the London Underground API. I'm struggling on parsing the GET request into something readable and where the user can access specific line information.
Here is my current code, I'm using a struct to store the response from the GET request after Unmarshaling it.
// struct for decoding into a structure
var tubeStatuses struct {
object []struct {
typeDef []string `json:"$type"`
idName string `json:"id"`
name string `json:"name"`
modeName string `json:"modeName"`
disruptions string `json:"disruption"`
created string `json:"created"`
modified string `json:"modified"`
statusObject []struct {
zeroObject []struct {
typeDef string `json:"$type"`
id int `json:"id"`
statusSeverity int `json:"statusSeverity"`
statusDesc string `json:"statusSeverityDescription"`
created string `json:"created"`
validity string `json:"validityPeriods"`
}
}
route string `json:"routeSections"`
serviceObject []struct {
zeroObject []struct {
typeDef string `json:"$type"`
name string `json:"name"`
uri string `json:"uri"`
}
}
crowdingObject []struct {
typeDef string `json:"$type"`
}
}
}
fmt.Println("Now retrieving Underground line status, please wait...")
// two variables (response and error) which stores the response from e GET request
getRequest, err := http.Get("https://api.tfl.gov.uk/line/mode/tube/status")
fmt.Println("The status code is", getRequest.StatusCode, http.StatusText(getRequest.StatusCode))
if err != nil {
fmt.Println("Error!")
fmt.Println(err)
}
//close - this will be done at the end of the function
// it's important to close the connection - we don't want the connection to leak
defer getRequest.Body.Close()
// read the body of the GET request
rawData, err := ioutil.ReadAll(getRequest.Body)
if err != nil {
fmt.Println("Error!")
fmt.Println(err)
}
jsonErr := json.Unmarshal(rawData, &tubeStatuses)
if jsonErr != nil {
fmt.Println(jsonErr)
}
//test
fmt.Println(tubeStatuses.object[0].name)
fmt.Println("Welcome to the TfL Underground checker!\nPlease enter a number for the line you want to check!\n0 - Bakerloo\n1 - central\n2 - circle\n3 - district\n4 - hammersmith & City\n5 - jubilee\n6 - metropolitan\n7 - northern\n8 - piccadilly\n9 - victoria\n10 - waterloo & city")
The error I see is the following:
json: cannot unmarshal array into Go value of type struct { object []struct { typeDef []string "json:\"$type\""; idName string "json:\"id\""; name string "json:\"name\""; modeName string "json:\"modeName\""; disruptions string "json:\"disruption\""; created string "json:\"created\""; modified string "json:\"modified\""; statusObject []struct { zeroObject []struct { typeDef string "json:\"$type\""; id int "json:\"id\""; statusSeverity int "json:\"statusSeverity\""; statusDesc string "json:\"statusSeverityDescription\""; created string "json:\"created\""; validity string "json:\"validityPeriods\"" } }; route string "json:\"routeSections\""; serviceObject []struct { zeroObject []struct { typeDef string "json:\"$type\""; name string "json:\"name\""; uri string "json:\"uri\"" } }; crowdingObject []struct { typeDef string "json:\"$type\"" } } }
How do I unmarshal the array into something readable?