用Node.js中的JSON对象响应(将对象数组转换为JSON字符串)

用Node.js中的JSON对象响应(将对象数组转换为JSON字符串),第1张

用Node.js中的JSON对象响应(将对象/数组转换为JSON字符串

在Express中使用res.json:

function random(response) {  console.log("response.json sets the appropriate header and performs JSON.stringify");  response.json({     anObject: { item1: "item1val", item2: "item2val" },     anArray: ["item1", "item2"],     another: "item"  });}

或者:

function random(response) {  console.log("Request handler random was called.");  response.writeHead(200, {"Content-Type": "application/json"});  var otherArray = ["item1", "item2"];  var otherObject = { item1: "item1val", item2: "item2val" };  var json = JSON.stringify({     anObject: otherObject,     anArray: otherArray,     another: "item"  });  response.end(json);}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5087705.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-16
下一篇2022-11-16

发表评论

登录后才能评论

评论列表(0条)

    保存