【发布时间】:2020-04-29 02:56:06
【问题描述】:
我正在构建一个从 London Underground API 读取信息的应用程序。我正在努力将 GET 请求解析为可读的内容以及用户可以访问特定行信息的位置。
这是我当前的代码,我使用一个结构来存储解组后来自 GET 请求的响应。
// 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")
我看到的错误如下:
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\"" } } }
如何将数组解组为可读的内容?
【问题讨论】:
-
我在下面添加了一个答案,但也推荐查看 golang 格式和命名标准,可以在这里找到:golang.org/doc/effective_go.html Go 是一门很棒的语言。玩得开心!