如何在Golang中进行嵌套的JSON响应?

时间:2019-03-04 14:09:20

标签: json postgresql go go-gorm

我是 Golang 的新手,需要帮助。

在我的 PostgreSQL 数据库中,我有4个表。他们叫:surveysquestionsoptionssurveys_questions_options。那就是他们的样子。

调查表:

| 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? |

OPTIONS 表:

| option_id (int)   | option_text (text) |
|-------------------|--------------------|
| 1                 | red                |
| 2                 | blue               |
| 3                 | grey               |
| 4                 | green              |
| 5                 | brown              |

SURVEYS_QUESTIONS_OPTIONS 表合并了前三个表中的数据:

| 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         |

如何在Golang中进行烦人的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"`
}

3 个答案:

答案 0 :(得分:1)

我不确定GORM的一部分,但是使用JSON时,您还需要在嵌套对象上添加struct标签:

type Survey struct {
  ...
  Questions []Question `json:"questions"`
}

type Question struct {
  ...
  Options []Option `json:"options"`
}

答案 1 :(得分:1)

我们在您的代码中缺少某些范围,因此很难为您指明正确的方向。您是要查询GORM以获得[]Survey,还是要编组[]Survey?无论如何,您也应该将标签添加到问题中,如slomek回答。

答案 2 :(得分:0)

但是,请尝试以下操作: 以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)