Ale*_*x B 6 javascript json httprequest node.js
所以我一般都在学习NodeJS和javascript,并且玩弄它并且我在解析JSON时遇到了一些问题.我从"用户"收到以下内容:
{
"sync_contact_list": [
{
"name": "c",
"number": "789",
"email": ""
},
{
"name": "b",
"number": "123",
"email": "a@a.com"
},
{
"name": "a",
"number": "416",
"email": ""
}
]
}
Run Code Online (Sandbox Code Playgroud)
我的问题是我如何正确地解析这个以得到各个位:
{
"name": "a",
"number": "416",
"email": ""
}
Run Code Online (Sandbox Code Playgroud)
我一直在尝试做var jsonObject = JSON.parse(req.body); 但是我不断解析错误,无论我如何改变我收到的JSON(单个组件,所有组件等).
任何人都可以指出我做错了什么吗?
编辑:所以我使用快递来处理不同的路径.所以我有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);
Run Code Online (Sandbox Code Playgroud)
然后我有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");
}
Run Code Online (Sandbox Code Playgroud)
该请求使用无线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
Run Code Online (Sandbox Code Playgroud)
编辑:我意识到我应该将内容类型更改为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)
Run Code Online (Sandbox Code Playgroud)
也许已经解析了?我不知道(我没看到你的整个代码).
如果是(或者你将按照你需要的方式解析它),那么特定的"位"将是可用的(请参阅此jsfiddle以获得证明):
for (var i=0; i<jsonObject['sync_contact_list'].length; i++){
// here jsonObject['sync_contact_list'][i] is your current "bit"
}
Run Code Online (Sandbox Code Playgroud)
在循环内,迭代jsonObject['sync_contact_list'][i]之间,"位"之间的变化,因为i更改并指向第一个元素,然后指向第二个,依此类推,直到最后一个元素.