我正在使用一个API,它的响应对于某个字段有时是对象,有时是对象数组。
我创建了一个结构体来解析json响应,它可以很好地工作。然而,在json响应包含对象数组的情况下,解析失败了。在Go中,我该如何处理这种情况呢?
Single Response:
{
"net": {
"comment": {
"line": {
"$": "This space is statically assigned",
"@number": "0"
}
}
}
}
Array Response:
{
"net": {
"comment": {
"line": [
{
"$": "All abuse issues will only be responded to by the Abuse",
"@number": "0"
},
{
"$": "Team through the contact info found on handle ABUSE223-ARIN",
"@number": "1"
}
]
}
}
}
我考虑创建2个结构体版本,然后以某种方式确定我得到了哪个实例,但这感觉非常浪费。我还尝试将其反序列化为map[string]instance{},但我有些迷失方向,不确定我是否走在正确的道路上。
如果有建议,请告诉我。