2014-04-08 88 views
1

我正在開發一個連接到MYSQL數據庫的node.js應用程序,獲取一些信息,將其轉換成JSON格式並將其寫入瀏覽器。Node.js mysql響應格式

我使用mysql npm(https://www.npmjs.org/package/mysql)。

connection.query("select tact, digits, mode from table1 where id = '"+id+"'", function(err, rows, fields){ 
    if (err){ 
     console.log(err); 
     throw err; 
    } 
    var objToJson = rows; 
    objToJson.response = response; 
    var finalresponse = JSON.stringify(objToJson); 
}); 

,最終的迴應是:

[{"tact":0,"digits":5,"mode":"on"}] 

的一點是,我只希望收到類似的信息(但應JSON解析):

[{0,5,"on"}] 

我怎麼能做它?可能嗎?

謝謝你們。

回答

1

要獲得每個值到一個數組,遍歷它:

connection.query("select tact, digits, mode from table1 where id = '"+id+"'", function(err, rows, fields){ 
    if (err){ 
     console.log(err); 
     throw err; 
    } 
    var objToJson = rows; 
    var response = []; 
    for (var key in rows) { 
     response.push(rows[key]); 
    } 
    objToJson.response = response; 
    var finalresponse = JSON.stringify(objToJson); 
});