处理各种参数类型的 Unmarshal 过程
时间:2024-03-20
浏览:438
作者:佚名
在使用 fritz!box 路由器 API 时,需要解组 JSON 响应并处理参数类型不一致的问题。某些情况下,wlan 参数是布尔值,而另一些情况下则是一个对象。文章提出了一种解决方案,即使用 json.RawMessage 将 wlan 属性的解组“延迟”到两个单独的结构字段。另一种解决方案是实现 json.Unmarshaler 和 json.Marshaler 接口,以便自定义参数的解组和编码行为,从而避免使用嵌套结构或额外的条件判断。
问题内容
我正在使用我的 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 存储库。
我需要添加布尔 wlan 参数,我尝试复制“data”结构并更改名称,但该解决方案对我来说听起来非常糟糕。
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"
}解决方案
我不知道这是否是一个好的解决方案,我还是个新手,但无论如何,您可以使用 json.RawMessage 并将 wlan 属性的解组“延迟”到两个单独的结构字段之一。例如:
package main
import (
"encoding/json"
"fmt"
)
// data contains data about the fritz!box. (other fields omitted for brevity)
type data struct {
language string `json:"language"`
newsurl string `json:"newsletter_url"`
wlanraw *json.rawmessage `json:"wlan"`
wlanbool bool `json:"-"`
wlaninfo *wlaninfo `json:"-"`
}
// wlaninfo contains infos about the wireless lan
type wlaninfo struct {
txt string `json:"txt"`
led string `json:"led"`
title string `json:"title"`
link string `json:"link"`
tooltip string `json:"tooltip"`
}
func unmarshaldata(raw []byte, data *data) error {
if err := json.unmarshal(raw, data); err != nil {
return err
}
switch string(*data.wlanraw) {
case "true", "false":
json.unmarshal(*data.wlanraw, &data.wlanbool)
default:
if err := json.unmarshal(*data.wlanraw, &data.wlaninfo); err != nil {
return err
}
}
return nil
}
func main() {
jsonbool := []byte(`
{
"language": "it",
"newsletter_url": "https://example.com/news",
"wlan": true
}`)
jsoninfo := []byte(`
{
"language": "it",
"newsletter_url": "https://example.com/news",
"wlan": {
"txt": "footxt",
"led": "fooled",
"title": "hello",
"link": "bar",
"tooltip": "baz"
}
}`)
// error handling omitted
var databool data
unmarshaldata(jsonbool, &databool)
fmt.printf("%+v\n\n", databool)
var datainfo data
unmarshaldata(jsoninfo, &datainfo)
fmt.printf("%+v %+v\n", datainfo, datainfo.wlaninfo)
}
$ go build fritz.go
$ ./fritz
{language:it newsurl:https://example.com/news wlanraw:0xc0000a4060 wlanbool:true wlaninfo:<nil>}
{language:it newsurl:https://example.com/news wlanraw:0xc0000a4080 wlanbool:false wlaninfo:0xc0000b0000} &{txt:footxt led:fooled title:hello link:bar tooltip:baz}
$您可以实现 json.Unmarshaler 和 json.Marshaler 接口。
type WLan struct {
Bool *bool `json:"-"`
Txt string `json:"txt"`
Led string `json:"led"`
Title string `json:"title"`
Link string `json:"link"`
Tooltip string `json:"tooltip"`
}
// implements json.Unmarshaler
func (w *WLan) UnmarshalJSON(data []byte) error {
if len(data) > 0 && (data[0] == 't' || data[0] == 'f') { // seems to be a bool
w.Bool = new(bool)
return json.Unmarshal(data, w.Bool)
}
if len(data) > 1 && data[0] == '{' && data[len(data)-1] == '}' { // it's an object
// type W and the conversion (*W)(w) are required to
// prevent encoding/json from invoking the UnmarshalJSON
// method recursively causing a stack overflow
type W WLan
return json.Unmarshal(data, (*W)(w))
}
return nil // or error, up to you
}
// implements json.Marshaler
func (w WLan) MarshalJSON() ([]byte, error) {
if w.Bool != nil {
return json.Marshal(*w.Bool)
}
// Same as with UnmarshalJSON, type W and the conversion W(w) are
// required to prevent encoding/json from invoking the MarshalJSON
// method recursively causing a stack overflow
type W WLan
return json.Marshal(W(w))
}
https://play.golang.org/p/s72zt4ny7Pv
今天关于《处理各种参数类型的 Unmarshal 过程》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!
本文内容来源于互联网,如有侵权请联系删除。
近期热门
11.18
站内热门
最新文章
热门专题
