如何使用Node Js创建嵌套的JSON响应

时间:2018-06-19 05:29:51

标签: mysql node.js api express

我正在使用Node.js和My-SQL来创建一个rest API。我希望我的响应像嵌套的JSON对象一样,在我的前端很容易阅读。我尝试使用for循环,但它没有给我正确的结果,是否有任何插件可用于格式化此响应或有任何简单的方法来执行此嵌套JSON响应。

我的SQL查询是

   private void jbtSaveActionPerformed(java.awt.event.ActionEvent evt) {                                        

    DefaultTableModel tablemodel = (DefaultTableModel) jTable6.getModel();
    LinkedList<String> paramList = new LinkedList<String>();

    for (int i = 0; i < tablemodel.getRowCount(); i++) {

        System.out.println("Table row count :" + tablemodel.getRowCount());
        System.out.println("i " + i);
        String rowValue = (String) tablemodel.getValueAt(i, 0);
        paramList.add(rowValue);

        System.out.println("row value :" + rowValue);
        System.out.println(paramList.get(i));

    }
    SamIntegerResult intRes1 = svc.saveTSAServices(token, tsaservice, paramList, idList);

    if (intRes1.isSuccess()) {
        JOptionPane.showMessageDialog(this, new JLabel("Saved successfully", JLabel.CENTER), "Save TSA Service", JOptionPane.PLAIN_MESSAGE);
    } else {
        JOptionPane.showMessageDialog(this, "Error saving to database", "Error", JOptionPane.ERROR_MESSAGE);
    }

} 

我得到的答案就像是

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.dialog, null);
dialogBuilder.setView(dialogView);

AlertDialog alertDialog = dialogBuilder.create();

但我需要

var sql ='SELECT comments.comment_id,comments.photo_id,comments.comment_message,comments.comment_date ,tb_users.username as comment_usename,tb_users.first_name as comment_firstname ,tb_users.profile_icon as comment_profile_icon,tb_reply.id,tb_reply.comment_id,tb_reply.photo_id,tb_reply.reply_message,tb_reply.create_date as reply_date,u2.username as r_usename,u2.first_name as r_first_name,u2.profile_icon as r_profile_icon '+
' FROM comments LEFT JOIN tb_reply ON tb_reply.comment_id=comments.comment_id '+
' INNER JOIN tb_users ON tb_users.id=comments.user_id '+
' LEFT JOIN tb_users u2 ON tb_reply.user_id = u2.id '+
' WHERE comments.photo_id= '+photoid+' ORDER BY comments.comment_id DESC LIMIT ' +(pageNo-1)*20+ ', 20 '

1 个答案:

答案 0 :(得分:0)

  1. 第一步查询所有注释使其等于变量调用结果
  2. 对于结果数组的循环,调用查询以将所有具有外键的回复加载到注释中并将结果保存为结果[x] .reply