【发布时间】:2021-04-04 15:36:00
【问题描述】:
我正在使用我的 Fritz!Box 路由器中的一些 API,我想在一个体面的结构中解组 json 响应,只需要找到一个好的方法来做到这一点。
有时在 API 响应中 WLan 参数是布尔值,有时是这种类型的对象
// WLan contains info about the Wireless Lan
type WLan struct {
Txt string `json:"txt"`
Led string `json:"led"`
Title string `json:"title"`
Link string `json:"link"`
Tooltip string `json:"tooltip"`
}
如果您需要有关代码的更多信息,可以使用github repo。
我需要添加布尔 wlan 参数,我尝试复制“数据”结构并更改名称,但该解决方案对我来说听起来很糟糕。
Wlan 包含在这个结构中:
// Data contains data about the Fritz!Box
type Data struct {
NasLink string `json:"naslink"`
FritzOS FritzOS `json:"fritzos"`
Webdav int `json:"webdav,string"`
Manual string `json:"MANUAL_URL"`
Language string `json:"language"`
AVM string `json:"AVM_URL"`
USBConnect string `json:"usbconnect"`
Foncalls Foncalls `json:"foncalls"`
VPN VPN `json:"vpn"`
Internet Internet `json:"internet"`
DSL DSL `json:"dsl"`
ServicePortalURL string `json:"SERVICEPORTAL_URL"`
Comfort Comfort `json:"comfort"`
Changelog Changelog `json:"changelog"`
TamCalls TamCalls `json:"tamcalls"`
Lan External `json:"lan"`
USB External `json:"usb"`
FonNum External `json:"fonnum"`
NewsURL string `json:"NEWSLETTER_URL"`
Net Net `json:"net"`
Dect External `json:"dect"`
WLan WLan `json:"wlan"`
//Wlan bool `json:"wlan"` # This is the other "case"
}
【问题讨论】:
-
这是在另一个结构中使用吗?如果是这样,也包括封闭结构。有多种方法可以解决此问题,但您需要在解组完成后阐明您想要什么。
-
是的,WLAN 包含在数据中。数据将“wlan”名称解组为 WLan,但有时不是 json 对象,而是一个 bool 值。无论如何你可以在 github 上看到完整的源代码。