admin管理员组

文章数量:1384579

I'm getting response from HTTP post as array of json objects. but want to display as string. Please see my response..

{ total: 14,
  results:
   [ { name: [Object],
       ments: [],
       attributes: [Object],
       type: [Object],
       context: [Object],
       status: 'Accepted',
       score: 1.7646775,
       modified: 1426085315767,
       parents: [] },
     { name: [Object],
       ments: [],
       attributes: [Object],
       type: [Object],
       context: [Object],
       status: 'Accepted',
       score: 1.3945999,
       modified: 1425386916807,
       parents: [] },

Below code to display response.

function(error, response, data){
    if(error) {
        console.log(error);
    } else {
          console.log(data);

}

I'm getting response from HTTP post as array of json objects. but want to display as string. Please see my response..

{ total: 14,
  results:
   [ { name: [Object],
       ments: [],
       attributes: [Object],
       type: [Object],
       context: [Object],
       status: 'Accepted',
       score: 1.7646775,
       modified: 1426085315767,
       parents: [] },
     { name: [Object],
       ments: [],
       attributes: [Object],
       type: [Object],
       context: [Object],
       status: 'Accepted',
       score: 1.3945999,
       modified: 1425386916807,
       parents: [] },

Below code to display response.

function(error, response, data){
    if(error) {
        console.log(error);
    } else {
          console.log(data);

}
Share Improve this question asked Nov 15, 2016 at 20:18 user2848031user2848031 22713 gold badges38 silver badges73 bronze badges 2
  • 4 Can you just use console.log(JSON.stringify(data))? – Travis J Commented Nov 15, 2016 at 20:19
  • You can do that with map: const arrAsStr = arr.map( (r) => JSON.stringify(r) ); – Hosar Commented Nov 15, 2016 at 21:50
Add a ment  | 

1 Answer 1

Reset to default 4

NodeJS has ES5 JSON class which has stringify() method in it

function(error, response, data){
    if(error) {
      console.log(error);
    } else {
      console.log(JSON.stringify(data));
}

JSON.stringify

本文标签: javascriptHow to convert from array of json object to String in NodejsStack Overflow