vì vậy tôi đang học NodeJS và javascript nói chung, và chơi xung quanh với nó và tôi có một số vấn đề phân tích cú pháp JSON. Tôi nhận được những điều sau đây từ "người sử dụng":Phân tích cú pháp các nút mảng JSON
{
"sync_contact_list": [
{
"name": "c",
"number": "789",
"email": ""
},
{
"name": "b",
"number": "123",
"email": "[email protected]"
},
{
"name": "a",
"number": "416",
"email": ""
}
]
}
Câu hỏi của tôi là làm thế nào tôi có thể đúng cách phân tích này để có được các bit riêng lẻ:
{
"name": "a",
"number": "416",
"email": ""
}
Tôi đã cố gắng để làm điều đó bằng cách làm var jsonObject = JSON.parse (req.body); , nhưng tôi tiếp tục nhận được lỗi phân tích cú pháp, bất kể tôi thay đổi JSON mà tôi nhận được như thế nào (các thành phần riêng lẻ, tất cả các thành phần, v.v ...).
Có ai có thể chỉ ra những gì tôi đang làm sai?
EDIT: Vì vậy, tôi sử dụng Express để xử lý các đường dẫn khác nhau. Vì vậy, tôi có 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);
Và sau đó tôi có index.js, nơi tôi về cơ bản có những điều sau cho mỗi tuyến đường.
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");
}
Yêu cầu được thực hiện với một dòng JSON miễn phí:
curl -i -d "{'sync_contact_list':[{'name':'c','number':'789','email':''},{'name':'b','number':'123','email':'[email protected]'},{'name':'a','number':'416','email':''}]}" http://localhost:3000/syncService
EDIT: Tôi nhận ra rằng tôi có lẽ nên thay đổi Type nội dung để application/json. trong trường hợp này, đối với JSON.stringify tôi nhận được thông tin sau:
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)
Lỗi phân tích cú pháp nói gì? Bạn phải phân tích cú pháp phản hồi đầy đủ và sau đó chỉ cần truy cập result.sync_contact_list [0] cho kết quả đầu tiên. – Daff
Hãy thử với một json linebreak-/spacefree. Điều đó có hiệu quả không? –
@AronWoost có thể đúng. Dữ liệu từ người dùng ** có chính xác ** ở định dạng bạn đã cung cấp không? Hoặc bạn chỉ cần định dạng nó cho StackOverflow? Tôi giả sử bạn chỉ cho chúng ta thấy cấu trúc của dữ liệu bạn muốn nhận, nhưng chúng ta nên đưa ra giá trị 'req.body' chính xác cùng với kiểu của nó (thử gọi' typeof req.body' và cho chúng ta kết quả). – Tadeck