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

您所在的位置:网站首页 慕课怎么绑定学号 如何在 Go 中进行嵌套的 JSON 响应?

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

#如何在 Go 中进行嵌套的 JSON 响应?| 来源: 网络整理| 查看: 265

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

在我的 PostgreSQL 数据库中,我有 4 个表。他们称:surveys、questions和options。surveys_questions_options

它们看起来像这样:

surveys桌子:

| survey_id (uuid4)                    | survey_name (varchar) |

|--------------------------------------|-----------------------|

| 0cf1cf18-d5fd-474e-a8be-754fbdc89720 | April                 |

| b9fg55d9-n5fy-s7fe-s5bh-856fbdc89720 | May                   |

questions桌子:

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

                    },



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3