parsingJSON数组nodejs

所以我正在学习NodeJS和一般的JavaScript,并玩弄它,我有一些parsingJSON的问题。 我从“用户”收到以下内容:

{ "sync_contact_list": [ { "name": "c", "number": "789", "email": "" }, { "name": "b", "number": "123", "email": "a@a.com" }, { "name": "a", "number": "416", "email": "" } ] } 

我的问题是我如何正确parsing这个来获取个人位:

 { "name": "a", "number": "416", "email": "" } 

我一直在尝试做var jsonObject = JSON.parse(req.body); ,但我不断收到parsing错误,不pipe我如何改变我收到的JSON(单个组件,所有的等)。

任何人都可以指出我做错了什么?

编辑:所以我使用快递处理不同的path。 所以我有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 

编辑:我意识到我应该更改内容types为应用程序/ 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) 

也许它已经被parsing了? 我不知道(我没有看到你的整个代码)。

如果是的话(或者你会按照你需要的方式进行parsing),那么具体的“比特”就可以像这样(见这个jsfiddle的certificate):

 for (var i=0; i<jsonObject['sync_contact_list'].length; i++){ // here jsonObject['sync_contact_list'][i] is your current "bit" } 

在循环中,在迭代之间, jsonObject['sync_contact_list'][i]在“位”之间变化,因为i改变并指向第一个元素,然后指向第二个元素,依此类推,直到最后一个元素。

所以事实certificate我的JSON格式不正确。 我从一个Java项目工作中得到的坏习惯。 一旦该部分被修复,我的请求被默认parsing,这导致了一些其他的头痛,直到我发现了节点检查。

因此,我selectTadeck作为正确的答案。