如何在Go中生成嵌套JSON响应?
我是新手,需要一些帮助 在我的PostgreSQL数据库中,我有4个表。他们称之为:如何在Go中生成嵌套JSON响应?,json,postgresql,go,go-gorm,Json,Postgresql,Go,Go Gorm,我是新手,需要一些帮助 在我的PostgreSQL数据库中,我有4个表。他们称之为:调查,问题,选项和调查问题 它们看起来像这样: [ { "survey_id": "0cf1cf18-d5fd-474e-a8be-754fbdc89720", "survey_name": "April", "questions": [ { "question_id": 1,
调查
,问题
,选项
和调查问题
它们看起来像这样:
[
{
"survey_id": "0cf1cf18-d5fd-474e-a8be-754fbdc89720",
"survey_name": "April",
"questions": [
{
"question_id": 1,
"question_text": "What is your favorite color?",
"options": [
{
"option_id": 1,
"option_text": "red"
},
{
"option_id": 2,
"option_text": "blue"
},
{
"option_id": 3,
"option_text": "grey"
},
]
}
]
},
{
"survey_id": "b9fg55d9-n5fy-s7fe-s5bh-856fbdc89720",
"survey_name": "May",
"questions": [
{
"question_id": 1,
"question_text": "What is your favorite color?",
"options": [
{
"option_id": 3,
"option_text": "grey"
},
{
"option_id": 4,
"option_text": "green"
},
{
"option_id": 5,
"option_text": "brown"
},
]
}
]
}
]
type Survey struct {
SurveyID string `gorm:"primary_key" json:"survey_id"`
SurveyName string `gorm:"not null" json:"survey_name"`
Questions []Question
}
type Question struct {
QuestionID int `gorm:"primary_key" json:"question_id"`
QuestionText string `gorm:"not null;unique" json:"question_text"`
Options []Option
}
type Option struct {
OptionID int `gorm:"primary_key" json:"option_id"`
OptionText string `gorm:"not null;unique" json:"option_text"`
}
调查
表格:
| survey_id (uuid4) | survey_name (varchar) |
|--------------------------------------|-----------------------|
| 0cf1cf18-d5fd-474e-a8be-754fbdc89720 | April |
| b9fg55d9-n5fy-s7fe-s5bh-856fbdc89720 | May |
| question_id (int) | question_text (text) |
|-------------------|------------------------------|
| 1 | What is your favorite color? |
| option_id (int) | option_text (text) |
|-------------------|--------------------|
| 1 | red |
| 2 | blue |
| 3 | grey |
| 4 | green |
| 5 | brown |
问题
表格:
| survey_id (uuid4) | survey_name (varchar) |
|--------------------------------------|-----------------------|
| 0cf1cf18-d5fd-474e-a8be-754fbdc89720 | April |
| b9fg55d9-n5fy-s7fe-s5bh-856fbdc89720 | May |
| question_id (int) | question_text (text) |
|-------------------|------------------------------|
| 1 | What is your favorite color? |
| option_id (int) | option_text (text) |
|-------------------|--------------------|
| 1 | red |
| 2 | blue |
| 3 | grey |
| 4 | green |
| 5 | brown |
选项
表格:
| survey_id (uuid4) | survey_name (varchar) |
|--------------------------------------|-----------------------|
| 0cf1cf18-d5fd-474e-a8be-754fbdc89720 | April |
| b9fg55d9-n5fy-s7fe-s5bh-856fbdc89720 | May |
| question_id (int) | question_text (text) |
|-------------------|------------------------------|
| 1 | What is your favorite color? |
| option_id (int) | option_text (text) |
|-------------------|--------------------|
| 1 | red |
| 2 | blue |
| 3 | grey |
| 4 | green |
| 5 | brown |
surveys\u questions\u options
table合并了前面三个表中的数据:
| survey_id | question_id | option_id |
|--------------------------------------|-------------|-----------|
| 0cf1cf18-d5fd-474e-a8be-754fbdc89720 | 1 | 1 |
| 0cf1cf18-d5fd-474e-a8be-754fbdc89720 | 1 | 2 |
| 0cf1cf18-d5fd-474e-a8be-754fbdc89720 | 1 | 3 |
| b9fg55d9-n5fy-s7fe-s5bh-856fbdc89720 | 1 | 3 |
| b9fg55d9-n5fy-s7fe-s5bh-856fbdc89720 | 1 | 4 |
| b9fg55d9-n5fy-s7fe-s5bh-856fbdc89720 | 1 | 5 |
如何在Go中生成嵌套JSON响应?我使用GORM库。我想要一个JSON响应,如下所示:
[
{
"survey_id": "0cf1cf18-d5fd-474e-a8be-754fbdc89720",
"survey_name": "April",
"questions": [
{
"question_id": 1,
"question_text": "What is your favorite color?",
"options": [
{
"option_id": 1,
"option_text": "red"
},
{
"option_id": 2,
"option_text": "blue"
},
{
"option_id": 3,
"option_text": "grey"
},
]
}
]
},
{
"survey_id": "b9fg55d9-n5fy-s7fe-s5bh-856fbdc89720",
"survey_name": "May",
"questions": [
{
"question_id": 1,
"question_text": "What is your favorite color?",
"options": [
{
"option_id": 3,
"option_text": "grey"
},
{
"option_id": 4,
"option_text": "green"
},
{
"option_id": 5,
"option_text": "brown"
},
]
}
]
}
]
type Survey struct {
SurveyID string `gorm:"primary_key" json:"survey_id"`
SurveyName string `gorm:"not null" json:"survey_name"`
Questions []Question
}
type Question struct {
QuestionID int `gorm:"primary_key" json:"question_id"`
QuestionText string `gorm:"not null;unique" json:"question_text"`
Options []Option
}
type Option struct {
OptionID int `gorm:"primary_key" json:"option_id"`
OptionText string `gorm:"not null;unique" json:"option_text"`
}
我的模型如下所示:
[
{
"survey_id": "0cf1cf18-d5fd-474e-a8be-754fbdc89720",
"survey_name": "April",
"questions": [
{
"question_id": 1,
"question_text": "What is your favorite color?",
"options": [
{
"option_id": 1,
"option_text": "red"
},
{
"option_id": 2,
"option_text": "blue"
},
{
"option_id": 3,
"option_text": "grey"
},
]
}
]
},
{
"survey_id": "b9fg55d9-n5fy-s7fe-s5bh-856fbdc89720",
"survey_name": "May",
"questions": [
{
"question_id": 1,
"question_text": "What is your favorite color?",
"options": [
{
"option_id": 3,
"option_text": "grey"
},
{
"option_id": 4,
"option_text": "green"
},
{
"option_id": 5,
"option_text": "brown"
},
]
}
]
}
]
type Survey struct {
SurveyID string `gorm:"primary_key" json:"survey_id"`
SurveyName string `gorm:"not null" json:"survey_name"`
Questions []Question
}
type Question struct {
QuestionID int `gorm:"primary_key" json:"question_id"`
QuestionText string `gorm:"not null;unique" json:"question_text"`
Options []Option
}
type Option struct {
OptionID int `gorm:"primary_key" json:"option_id"`
OptionText string `gorm:"not null;unique" json:"option_text"`
}
我不确定GORM部分,但对于JSON,您还需要在嵌套对象上添加结构标记:
type Survey struct {
...
Questions []Question `json:"questions"`
}
type Question struct {
...
Options []Option `json:"options"`
}
我不确定GORM部分,但对于JSON,您还需要在嵌套对象上添加结构标记:
type Survey struct {
...
Questions []Question `json:"questions"`
}
type Question struct {
...
Options []Option `json:"options"`
}
我们在您的代码中缺少一些作用域,因此很难为您指出正确的方向。您是询问查询GORM以便获得
[]调查
,还是询问编组[]调查
?不管怎样,你也应该在问题中添加标签,正如slomek回答的那样 您的代码缺少一些作用域,因此很难为您指出正确的方向。您是询问查询GORM以便获得[]调查
,还是询问编组[]调查
?不管怎样,你也应该在问题中添加标签,正如slomek回答的那样 但是,请尝试以下方法:
获取m2m关系中的嵌套数据
type Survey struct {
gorm.Model
SurveyID string `gorm:"primary_key" json:"survey_id"`
SurveyName string `gorm:"not null" json:"survey_name"`
Questions []*Question `gorm:"many2many:survey_questions;"`
}
surveys := []*model.Survey{}
db := dbSession.Where(&model.Survey{SurveyID: id}).Preload("Questions").Find(&surveys)
但是,请尝试以下方法:
获取m2m关系中的嵌套数据
type Survey struct {
gorm.Model
SurveyID string `gorm:"primary_key" json:"survey_id"`
SurveyName string `gorm:"not null" json:"survey_name"`
Questions []*Question `gorm:"many2many:survey_questions;"`
}
surveys := []*model.Survey{}
db := dbSession.Where(&model.Survey{SurveyID: id}).Preload("Questions").Find(&surveys)
你想在哪里解决这个问题?@Tinwor你好!首先,我需要正确的方向。你认为我的模型(结构)正确吗?第二,我是否需要struct来维持我的表?如果我的模型是正确的,我如何在处理程序中正确使用它们来创建乏味的JSON响应。如果可能,需要一些示例。如果封送嵌套结构,它将生成嵌套JSON。您的问题具体是什么?为
问题添加相关标签
/选项
,然后整理调查
,应该会得到您想要的结果!好的,我修复了我的模型(结构)。下一步怎么办?你想在哪里解决这个问题?@Tinwor你好!首先,我需要正确的方向。你认为我的模型(结构)正确吗?第二,我是否需要struct来维持我的表?如果我的模型是正确的,我如何在处理程序中正确使用它们来创建乏味的JSON响应。如果可能,需要一些示例。如果封送嵌套结构,它将生成嵌套JSON。您的问题具体是什么?为问题添加相关标签
/选项
,然后整理调查
,应该会得到您想要的结果!好的,我修复了我的模型(结构)。接下来呢?你好!我对这两种解决方案都感兴趣。我不确定是否可以使用GORM查询,所以最好使用编组。您好!我对这两种解决方案都感兴趣。我不确定是否可以使用GORM查询,所以最好使用编组。您好!好的,但是下一步呢?据我所知,我需要对表SURVEYS\u QUESTIONS\u OPTIONS
进行SQL查询。然后通过编组来包装结果,对吗?真的很困惑。你好!好的,但是下一步呢?据我所知,我需要对表SURVEYS\u QUESTIONS\u OPTIONS
进行SQL查询。然后通过编组来包装结果,对吗?真的很困惑。