Javascript 制作JSON代码';显示整洁';在HTML页面上
我正试图让这段代码更清晰,它确实显示在html页面上Javascript 制作JSON代码';显示整洁';在HTML页面上,javascript,html,json,Javascript,Html,Json,我正试图让这段代码更清晰,它确实显示在html页面上 app.get("/web/users", function(req, res) { var users = app.models.users.model('User'); users.find({}).lean().exec(function(err, result){ res.write(JSON.stringify(result)); res.end(); 这确实有效,它列出了如下内容 [{“_id”:“…”,“name”:“Just
app.get("/web/users", function(req, res) {
var users = app.models.users.model('User');
users.find({}).lean().exec(function(err, result){
res.write(JSON.stringify(result));
res.end();
这确实有效,它列出了如下内容
[{“_id”:“…”,“name”:“Justin”,“phonenumber”:“5555”,“useSMS”:true,“callerId”:[“…”,“PIN”:“…”]
现在,我希望这个代码显示如下
Justin 5555如果我理解正确,并且您希望您的json打印精美,请点击这里:
res.write(JSON.stringify(result, null, 2));
这将使用2个空格按层次缩进JSON
编辑:重新阅读Mouseroot的评论,我认为你真正想要的是:
res.write(result.name + ' ' + result.phonenumber);
现在你只需要把你的json字符串当作一个对象,例如:
result.name
和result.phonenumber
@Mouseroot哦,我想我误解了这个问题,因为他使用了result=json.stringify(result)
他可以将JSON字符串视为一个对象,如果您在chrome中,您可以执行console.dir(result)
以更好地了解更复杂JSON对象的结构。