Javascript Node.js:在客户端处理JSON对象
我在使用socket.io处理Node.js服务器客户端的JSON对象时遇到问题 这是我发送数据的方式,我正在读取的文件具有JSON格式:Javascript Node.js:在客户端处理JSON对象,javascript,json,node.js,Javascript,Json,Node.js,我在使用socket.io处理Node.js服务器客户端的JSON对象时遇到问题 这是我发送数据的方式,我正在读取的文件具有JSON格式: io.on('connection', function(socket){ console.log("A user connected"); fs.readFile('/.../file.json', 'utf-8', function(err, data){ socket.emit('news', JSON.stringif
io.on('connection', function(socket){
console.log("A user connected");
fs.readFile('/.../file.json', 'utf-8', function(err, data){
socket.emit('news', JSON.stringify(data));
});
socket.on('disconnect', function(){
console.log("A user disconnected");
});
});
我调用JSON.stringify以便为二进制数据提供可读的格式
现在,在客户端,html的内部有以下块:
根据JSON Lint的说法,这是一个有效的JSON,但是当我试图访问时间戳标记时,我仍然没有定义,客户机和服务器之间应该没有关系。事实上,您使用的node.js恰好是JavaScript,这与客户端无关 如果您在C或Java中执行了相同的操作,这对客户机是否重要
总之,第1步应该是验证您的JSON对象是否在客户端上正确接收。然后,使用诸如的验证器确保JSON是有效的对象。如果无效,则问题出在服务器端。如果没有,您需要检查客户端的解析。我发送了错误的数据格式,正确的代码是:
io.on('connection', function(socket){
console.log("A user connected");
fs.readFile('/.../file.json', 'utf-8', function(err, data){
socket.emit('news', data.toString());
});
socket.on('disconnect', function(){
console.log("A user disconnected");
});
});
我在两侧调用JSON.parse来检查我处理的数据是否正确,并且没有收到任何错误。我还复制粘贴了console.logobj在JSON Lint中的输出,它说这是一个有效的jsonMaybe,如果它有效,您应该接受自己的答案;我试过了,但它说我需要在2天内回来接受它。那我就做;
{
"timestamp": "Wed Aug 27 13:14:01 CEST 2014",
"devices": [
{
"A": {
"mac": "00:07:80:68:18:41",
"handles": [
{
"TxHandler1": "0418",
"TxHandler2": "020f00072a",
"TxHandler3": "bd",
"a": {
"hnd": "0x0010",
"uuid": "00002800-0000-1000-8000-00805f9b34fb",
"value": "0a 18 "
},
"b": {
"hnd": "0x0011",
"uuid": "00002803-0000-1000-8000-00805f9b34fb",
"value": "02 12 00 29 2a "
},
"c": {
"hnd": "0x0012",
"uuid": "00002a29-0000-1000-8000-00805f9b34fb",
"value": "56 4c "
},
"d": {
"hnd": "0x0013",
"uuid": "00002901-0000-1000-8000-00805f9b34fb",
"value": "46 41 "
},
"e": {
"hnd": "0x0014",
"uuid": "00002803-0000-1000-8000-00805f9b34fb",
"value": "02 15 00 24 2a "
},
"f": {
"hnd": "0x0015",
"uuid": "00002a24-0000-1000-8000-00805f9b34fb",
"value": "31 31 "
},
"g": {
"hnd": "0x0016",
"uuid": "00002901-0000-1000-8000-00805f9b34fb",
"value": "4d 4f 44 "
}
}
]
}
},
{
"B": {
"mac": "00:07:80:68:18:8E",
"handles": [
{
"TxHandler1": "0418",
"TxHandler2": "020f00072a",
"TxHandler3": "bd",
"a": {
"hnd": "0x0010",
"uuid": "00002800-0000-1000-8000-00805f9b34fb",
"value": "0a 18 "
},
"b": {
"hnd": "0x0011",
"uuid": "00002803-0000-1000-8000-00805f9b34fb",
"value": "02 12 00 29 2a "
},
"c": {
"hnd": "0x0012",
"uuid": "00002a29-0000-1000-8000-00805f9b34fb",
"value": "56 4c "
},
"d": {
"hnd": "0x0013",
"uuid": "00002901-0000-1000-8000-00805f9b34fb",
"value": "46 41 "
},
"e": {
"hnd": "0x0014",
"uuid": "00002803-0000-1000-8000-00805f9b34fb",
"value": "02 15 00 24 2a "
},
"f": {
"hnd": "0x0015",
"uuid": "00002a24-0000-1000-8000-00805f9b34fb",
"value": "31 31 "
},
"g": {
"hnd": "0x0016",
"uuid": "00002901-0000-1000-8000-00805f9b34fb",
"value": "4d 4f 44 "
}
}
]
}
}
]
}
io.on('connection', function(socket){
console.log("A user connected");
fs.readFile('/.../file.json', 'utf-8', function(err, data){
socket.emit('news', data.toString());
});
socket.on('disconnect', function(){
console.log("A user disconnected");
});
});