Convert sql object to valid Json string in node.js - Azure

馋奶兔 提交于 2019-12-19 04:18:10

问题


We are creating a web service in Azure service using node.js to retrieve data from SQL db. We are using ClearDB to do the same.

While retriving the data its not comming in a proper JSON format. How can we convert the result sql object to JSON string.

Below is my code.

app.get('/android', function(request, response) {
    pool.getConnection(function(err, connection) {
        if(err) { handleErrorResponse(err, response); return; }
            var sql = "select projectname from taggedemployee where empname='test@hotmail.com' and tagflag='accepted'"
        connection.query(sql, {}, function(err, results) {
              connection.release(); // always put connection back in pool after last query
          if(err) { handleErrorResponse(err, response); return;  }
                var proj = JSON.stringify(results);
                console.log(proj);
                console.log(proj[0].projectname);
                 for(var myKey in proj) {
                    console.log("key:"+ myKey+", value:"+proj[myKey]);
                 }
          response.setHeader('Content-Type', 'application/json');
          response.status(200).send(JSON.stringify(results) );

        });
    });
});

I cant manipulate the JSON string the returning string is

[{projectname: "Dominos"}]

I tried JSON.stringify but no luck. Please help me to fix this issue


回答1:


You don't need JSON.stringify() actually. results is already your javascript object, which represents array of json objects. Just use

console.log(results[0].projectname);



回答2:


The JavaScript Object or Array is JSON, and you need to convert a JSON string to a JavaScript Object thru the function eval or JSON.parse. Please refer to http://www.json.org/js.html.




回答3:


The response from the SQL service is JSON - as you have shown. You need to use JSON.parse() to parse the JSON into an object. Something like:

app.get('/android', function(request, response) {
    pool.getConnection(function(err, connection) {
        if(err) { handleErrorResponse(err, response); return; }
            var sql = "select projectname from taggedemployee where empname='test@hotmail.com' and tagflag='accepted'"
        connection.query(sql, {}, function(err, results) {
              connection.release(); // always put connection back in pool after last query
          if(err) { handleErrorResponse(err, response); return;  }
                var proj = JSON.parse(response);
                console.log(proj);
          response.setHeader('Content-Type', 'application/json');
          response.status(200).send(results);

        });
    });
});

JSON.stringify is used to convert an object into a JSON string. JSON.parse is used to convert a JSON string into an object.



来源:https://stackoverflow.com/questions/33797867/convert-sql-object-to-valid-json-string-in-node-js-azure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!