Javascript/Json:转换为关联数组的对象的对象
使用AngularJS,我将以下数据发送到我的API:Javascript/Json:转换为关联数组的对象的对象,javascript,json,angularjs,node.js,express,Javascript,Json,Angularjs,Node.js,Express,使用AngularJS,我将以下数据发送到我的API: $http.post('/api/test', { credits: { value:"100", action:"test" } }); 在我的nodeJS(+Express)后端中,我获得以下数据: 为什么我的帖子数据已转换为关联数组 我想要的是: credits : Object action : "test" velue : "100" 使用包,选项“extended”为true 例如: var ex
$http.post('/api/test', { credits: { value:"100", action:"test" } });
在我的nodeJS(+Express)后端中,我获得以下数据:
为什么我的帖子数据已转换为关联数组
我想要的是:
credits : Object
action : "test"
velue : "100"
使用包,选项“extended”为true
例如:
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.post('/api/test',function (req, res) {
var data = req.body;
console.log(data);
console.log(data.credits);
console.log(data.credits.value);
console.log(data.credits.action);
var result = "";
for (var key in data.credits) {
if (data.credits.hasOwnProperty(key)) {
console.log(key + " -> " + data.credits[key]);
result += key + " -> " + data.credits[key];
}
}
res.send(result);
res.end();
});
app.listen(process.env.PORT || 3000, process.env.IP || "0.0.0.0", function(){
});
控制台中的结果
{ credits: { value: '100', action: 'test' } }
{ value: '100', action: 'test' }
100
test
value -> 100
action -> test
我建议使用主体解析器,你在使用express吗?是的,我在使用express,很抱歉没有提及。docs@davcs86中有一个例子:这是否意味着我没有做任何“错误”的事情,这是本机主体解析器的一种古怪行为?
{ credits: { value: '100', action: 'test' } }
{ value: '100', action: 'test' }
100
test
value -> 100
action -> test