我正在用node.js练习代码。我想将数组项转换为 JSON 格式。我也尝试过 body-parser 但无法成功。
var express=require('express');
var app= express();
var bodyParser= require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:false}));
var items = [
{"id":"sd223",
"text":"food1"},
{"id":"sd12223",
"text":"food2"},
{"id":"s4d223",
"text":"food3"},
{"id":"s65d223",
"text":"food4"},
];
app.get('/',function(request,response)
{
response.send(items);
});
app.listen(3000,function(){
console.log("server 3000");
});
最佳答案
如果您想以 json
形式发送响应,请使用 response.json(items)
。如果您只想将其转换为 JSON 字符串,请使用 JSON.stringify(items)
关于javascript - 我的 js 文件中的数组未转换为 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57544378/