我想创建一个 JSON 响应,其中父对象的字段名称具有动态名称。
最好用一个例子来解释。
现在,我的回复是这样的:
{
"userId": 1,
"id": 1,
"title": "delectus aut autem",
"completed": false
}
为此,我创建了一个结构 -> 填充它 -> 返回它。
我的结构如下所示:
type Placeholder struct {
userId int `json:"userId"`
id int `json:"id"`
title string `json:"title"`
completed bool `json:"completed"`
}
//...
res := Placeholder{
userId: 1,
id: 1,
title: "delectus aut autem",
completed: false,
}
现在我想使用 userId 作为字段名。所以我想要这个结果:
{
"1": {
"id": 1,
"title": "delectus aut autem",
"completed": false
}
}
有没有可能在 Go 中做到这一点?
开心每一天1111
浮云间
相关分类