我正在学习NodeJS和javascript,尝试使用它并遇到了一些解析JSON的问题。我从“用户”那里收到以下内容:
{
"sync_contact_list": [
{
"name": "c",
"number": "789",
"email": ""
},
{
"name": "b",
"number": "123",
"email": "a@a.com"
},
{
"name": "a",
"number": "416",
"email": ""
}
]
}
我的问题是如何正确解析这个数据以获取单独的位:
{
"name": "a",
"number": "416",
"email": ""
}
我一直在尝试通过 var jsonObject = JSON.parse(req.body); 来实现,但无论我如何变化接收到的JSON(单独组件、全部等),都会出现解析错误。
有人能指出我哪里做错了吗?
编辑: 所以我使用express来处理不同的路径。所以我有一个app.js:
var express = require('express')
, routes = require('./routes')
//var mysql = require('mysql');
var app = module.exports = express.createServer();
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
//app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
// Routes
//app.post('/', routes.syncServiceIndex);
app.post('/syncService', routes.synchServicePost);
app.get('/syncService/:syncServiceUser/sync', routes.synchServiceSync);
app.post('/syncService/:syncServiceUser/push', routes.synchServicePush);
app.del('/syncService/:syncServiceUser', routes.synchServiceDel);
app.post('/syncService/:syncServiceUser/contacts/push', routes.synchServiceContactsPush);
app.get('/syncService/:syncServiceUser/contacts/sync', routes.synchServiceContactsSync);
app.post('/syncService/:syncServiceUser/contacts/', routes.synchServiceContactsPost);
app.get('/syncService/:syncServiceUser/contacts/:contactId', routes.synchServiceContactsGet);
app.put('/syncService/:syncServiceUser/contacts/:contactId', routes.synchServiceContactsPut);
app.del('/syncService/:syncServiceUser/contacts/:contactId', routes.synchServiceContactsDel);
app.listen(3000);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
然后我有一个index.js,对于每个路由大致如下。
exports.synchServicePost = function(req, res) {
console.log('synchServicePost');
console.log("BODY:"+JSON.stringify(req.body));
var jsonObject = JSON.parse(req.body);
res.statusCode = 200;
res.send("OK\n");
}
请求使用一行无空格的JSON进行。
curl -i -d "{'sync_contact_list':[{'name':'c','number':'789','email':''},{'name':'b','number':'123','email':'a@a.com'},{'name':'a','number':'416','email':''}]}" http://localhost:3000/syncService
编辑:我意识到应该将内容类型更改为application/json。在这种情况下,对于JSON.stringify,我得到以下结果:
SyntaxError: Unexpected token ILLEGAL
at parse (native)
at IncomingMessage.<anonymous> (/home/alex/peekcode/quipmail/synch/node_modules/express/node_modules/connect/lib/middleware/bodyParser.js:71:15)
at IncomingMessage.emit (events.js:61:17)
at HTTPParser.onMessageComplete (http.js:133:23)
at Socket.ondata (http.js:1029:22)
at Socket._onReadable (net.js:677:27)
at IOWatcher.onReadable [as callback] (net.js:177:10)
req.body
值以及其类型(尝试调用typeof req.body
并给出结果)。 - Tadeck'{"sync_contact_list":[{"name":"c","number":"789","email":""},{"name":"b","number":"123","email":"a@a.com"},{"name":"a","number":"416","email":""}]}'
- RightSaidFred