doubishi8303 2019-03-05 07:48
浏览 196
已采纳

如何返回嵌套的JSON?

I have 3 table in PostgreSQL database.

QUESTIONS table:

| id (int) | text (text)                          |
|----------|--------------------------------------|
| 1        | What is your favorite color?         |
| 2        | What is your favorite football club? |

OPTIONS table:

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

QUESTIONS_OPTIONS table:

| question_id (int) | option_id (int) |
|-------------------|-----------------|
| 1                 | 1               |
| 1                 | 2               |
| 1                 | 3               |
| 1                 | 4               |
| 1                 | 5               |

In Golang application I create such models:

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

In controller I have such code:

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

        if err := rows.Scan(&question.ID, &question.Text, options); err != nil {  // service raise error in this line: sql: expected 2 destination arguments in Scan, not 3
            log.Println(err)
        }

        questions = append(questions, question)
    }
    utils.Response(responseWriter, http.StatusOK, questions)
}

When I try to make GET request to take all questions with all there options service such incorrect result:

[
    {
        "question_id": 0,
        "question_text": "",
        "options": null
    },
    {
        "question_id": 0,
        "question_text": "",
        "options": null
    }
]

Where I make mistake?

  • 写回答

1条回答 默认 最新

  • doudonglu3764 2019-03-05 08:01
    关注

    You should move queries.Close() to the end of loop, like this:

    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)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?