为了账号安全,请及时绑定邮箱和手机立即绑定

如何返回嵌套的 JSON?

如何返回嵌套的 JSON?

Go
浮云间 2023-06-19 11:14:43
我在PostgreSQL数据库中有 3 个表。问题表:| id (int) | text (text)                          ||----------|--------------------------------------|| 1        | What is your favorite color?         || 2        | What is your favorite football club? |选项表:| id (int) | text (text) ||----------|-------------|| 1        | red         || 2        | blue        || 3        | grey        || 4        | green       || 5        | brown       |QUESTIONS_OPTIONS表:| question_id (int) | option_id (int) ||-------------------|-----------------|| 1                 | 1               || 1                 | 2               || 1                 | 3               || 1                 | 4               || 1                 | 5               |在 Golang 应用程序中,我创建了这样的模型:type Option struct {    ID   int    `json:"option_id"`    Text string `json:"option_text"`}type Question struct {    ID int `json:"question_id"`    Text string `json:"question_text"`    Options []Option `json:"options"`}在控制器中我有这样的代码:var GetQuestions = func(responseWriter http.ResponseWriter, request *http.Request) {    rows, _ := database.DBSQL.Query("SELECT * FROM questions;")    defer rows.Close()    var questions []Question    for rows.Next() {        var question Question        var options []Option        queries, _ := database.DBSQL.Query(`select options.id as option_id, options.text as option_text from questions_options inner join questions on questions_options.question_id = ` + &question.ID + ` inner join options on questions_options.option_id = options.id`)        queries.Close()        for queries.Next() {            var option Option            if err := queries.Scan(&option.ID, &option.Text); err != nil {                log.Println(err)            }            options = append(options, option)        }我哪里弄错了?
查看完整描述

1 回答

?
冉冉说

TA贡献1877条经验 获得超1个赞

你应该移动queries.Close()到循环的末尾,像这样:


var GetQuestions = func(responseWriter http.ResponseWriter, request *http.Request) {

    rows, _ := database.DBSQL.Query("SELECT * FROM questions;")

    defer rows.Close()


    var questions []Question


    for rows.Next() {

        var question Question


        if err := rows.Scan(&question.ID, &question.Text); err != nil {

            log.Println(err)

            continue

        }


        queries, _ := database.DBSQL.Query(`select options.id as option_id, options.text as option_text from questions_options inner join questions on questions_options.question_id = $1 inner join options on questions_options.option_id = options.id`, question.ID)


        for queries.Next() {

            var option Option

            if err := queries.Scan(&option.ID, &option.Text); err != nil {

                log.Println(err)

            }

            question.Options = append(question.Options, option)

        }

        queries.Close()


        questions = append(questions, question)

    }

    utils.Response(responseWriter, http.StatusOK, questions)

}


查看完整回答
反对 回复 2023-06-19
  • 1 回答
  • 0 关注
  • 105 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信