混淆了 Golang 中的结构数组
时间:2024-03-05
浏览:373
作者:佚名
欢迎各位小伙伴来到golang学习网,相聚于此都是缘哈哈哈!今天我给大家带来《混淆了 Golang 中的结构数组》,这篇文章主要讲到等等知识,如果你对Golang相关的知识非常感兴趣或者正在自学,都可以关注我,我会持续更新相关文章!当然,有什么建议也欢迎在评论留言提出!一起学习!
我正在尝试与 json api 进行交互。它有两个端点:
gettraveltimeasjson - 指定旅行时间 id,它返回单个旅行时间 gettraveltimesasjson - 返回包含上述所有 traveltimes 的数组。
所以我有一个像这样的结构:
type traveltime struct {
averagetime int `json:"averagetime"`
currenttime int `json:"currenttime"`
description string `json:"description"`
distance float64 `json:"distance"`
endpoint struct {
description string `json:"description"`
direction string `json:"direction"`
latitude float64 `json:"latitude"`
longitude float64 `json:"longitude"`
milepost float64 `json:"milepost"`
roadname string `json:"roadname"`
} `json:"endpoint"`
name string `json:"name"`
startpoint struct {
description string `json:"description"`
direction string `json:"direction"`
latitude float64 `json:"latitude"`
longitude float64 `json:"longitude"`
milepost float64 `json:"milepost"`
roadname string `json:"roadname"`
} `json:"startpoint"`
timeupdated string `json:"timeupdated"`
traveltimeid int `json:"traveltimeid"`
}
如果我在一次旅行中像这样调用 api,我会得到一个填充的结构(我正在使用这个 req lib)
header := req.header{
"accept": "application/json",
"accept-encoding": "gzip",
}
r, _ := req.get("http://www.wsdot.com/traffic/api/traveltimes/traveltimesrest.svc/gettraveltimeasjson?accesscode=<redacted>&traveltimeid=403", header)
var foo traveltime
r.tojson(&foo)
dump.dump(foo)
如果我转储响应,它看起来像这样:
traveltime {
averagetime: 14 (int),
currenttime: 14 (int),
description: "sb i-5 pierce king county line to sr 512",
distance: 12.06 (float64),
endpoint: {
description: "i-5 @ sr 512 in lakewood",
direction: "s",
latitude: 47.16158351 (float64),
longitude: -122.481133 (float64),
milepost: 127.35 (float64),
roadname: "i-5"
},
name: "sb i-5, pkcl to sr 512",
startpoint: {
description: "i-5 @ pierce king county line",
direction: "s",
latitude: 47.255624 (float64),
longitude: -122.33113 (float64),
milepost: 139.41 (float64),
roadname: "i-5"
},
timeupdated: "/date(1532707200000-0700)/",
traveltimeid: 403 (int)
}
现在,我想做的是为所有响应创建一个结构,它是 traveltime 结构的一部分,所以我这样做了:
type traveltimesresponse struct {
traveltime []traveltime
}
但是,当我调用 gettraveltimesasjson 端点并将其更改为:
var foo traveltimesresponse
我得到了 180 个(结果数)空集,如下所示:
{
traveltime: traveltime {
averagetime: 0 (int),
currenttime: 0 (int),
description: "",
distance: 0 (float64),
endpoint: {
description: "",
direction: "",
latitude: 0 (float64),
longitude: 0 (float64),
milepost: 0 (float64),
roadname: ""
},
name: "",
startpoint: {
description: "",
direction: "",
latitude: 0 (float64),
longitude: 0 (float64),
milepost: 0 (float64),
roadname: ""
},
timeupdated: "",
traveltimeid: 0 (int)
}
json 在这里:https://gist.github.com/jaxxstorm/0ab818b300f65cf3a46cc01dbc35bf60
如果我将原始 traveltime 结构修改为像这样的切片,它就会起作用:
type TravelTimes []struct {
}
但是它不能作为单个响应工作。
我以前曾成功做到过这一点,但由于某种原因,这次失败让我的大脑失败了。任何帮助表示赞赏。
解决方案
更改 struct traveltimesresponse 字段名称 traveltime ,该字段名称与 struct 名称 traveltime 类似,然后尝试解组 json。
type traveltimesresponse struct {
traveltime []traveltime
}
上面应该有不同的字段名称:
type traveltimesresponse struct {
traveltimedata []traveltime
}
[已编辑]
library 直接使用 interface{} 来解组响应
// tojson convert json response body to struct or map
func (r *resp) tojson(v interface{}) error {
data, err := r.tobytes()
if err != nil {
return err
}
return json.unmarshal(data, v)
}
给定的 json 是一个对象数组。并且您将其解析为带有字段名称的结构,该字段名称将成为对象键。创建结构体切片或实现 unmarshal。
Unmarshal 在接口值中存储以下之一:
bool, for json booleans
float64, for json numbers
string, for json strings
[]interface{}, for json arrays
map[string]interface{}, for json objects
nil for json null
更好的是使用 go 包来实现 encoding/json。
尝试在 Go playground 上运行代码
解组不起作用,因为它期望顶层是具有字段 traveltime 的对象,而顶层实际上是对象数组。您只想直接解组到对象切片中,如下所示:
var foo []TravelTime r.ToJSON(&foo)
好了,本文到此结束,带大家了解了《混淆了 Golang 中的结构数组》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!
本文内容来源于互联网,如有侵权请联系删除。
近期热门
站内热门
最新文章
热门专题
