【发布时间】:2015-11-10 02:44:42
【问题描述】:
我正在使用一个 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{},但我有点迷茫,不确定我是否走在正确的道路上。
任何建议将不胜感激。
【问题讨论】:
-
再次感谢@JimB 我使用stackoverflow.com/questions/32346117/… 来解决这个问题。我花了一些时间来理解它,但你去吧。