登录
首页 >  Golang >  Go问答

在 Go 中如何实现 JSON 响应的嵌套结构?

来源:stackoverflow

时间:2024-03-03 22:24:28 218浏览 收藏

本篇文章主要是结合我之前面试的各种经历和实战开发中遇到的问题解决经验整理的,希望这篇《在 Go 中如何实现 JSON 响应的嵌套结构?》对你有很大帮助!欢迎收藏,分享给更多的需要的朋友学习~

问题内容

我是 go 新手,需要一些帮助。

在我的 postgresql 数据库中,我有 4 个表。他们称为:surveysquestionsoptionssurveys_questions_options

它们看起来像这样:

surveys 表:

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

如何在 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"`
}

解决方案


我不确定 gorm 部分,但使用 json,您还需要在嵌套对象上添加结构标签:

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

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

文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《在 Go 中如何实现 JSON 响应的嵌套结构?》文章吧,也可关注golang学习网公众号了解相关技术文章。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>