【发布时间】:2016-11-09 11:03:19
【问题描述】:
我在将以下格式的 json 数据解组为结构时遇到问题。 json 的结构对我来说看起来有点混乱,所以为我为解组它所做的所有愚蠢的事情道歉。
{
"message": {
"Server1.example.com": [
{
"application": "Apache",
"host": {
"name": "/^Server-[13456]/"
},
"owner": "User1",
"project": "Web",
"subowner": "User2"
}
],
"Server2.example.com": [
{
"application": "Mysql",
"host": {
"name": "/^Server[23456]/"
},
"owner": "User2",
"project": "DB",
"subowner": "User3"
}
]
},
"response_ms": 659,
"success": true
}
我正在尝试使用以下结构对其进行解组。
type ServerDetails struct {
Message struct{
Hostname struct{
Details struct{
Application string `json:"application"`
}`json:"-"`
}`json:"-"`
}`json:"message"`
}
字段Server[0-9].example.com在生成的时候是未知的,会变化,有这个字段
{
"application": "Apache",
"host": {
"name": "/^Server-[13456]/"
},
就在没有外部密钥的服务器名称之后,这让我再次感到困惑。我尝试了很多组合来了解如何解组,但我失败了。
将 json 字段解组为结构的工作方法是什么?
【问题讨论】:
-
使用
map[string]whateverstruct用各种键解组对象。 -
谢谢@Volker,你拯救了我的一天。我相应地进行了修改并且它起作用了。您能否将其添加为答案,我会接受。
标签: json go unmarshalling