2014-08-27 121 views
1

我在使用socket.ioNode.js服务器的客户端处理JSON对象时遇到问题。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.stringify(data)); 
    }); 
    socket.on('disconnect', function(){ 
     console.log("A user disconnected"); 
    }); 
}); 

我为了给一个可读的格式设置为二进制data调用JSON.stringify。现在

,在客户端,我有内部的以下<script>块中的HTML的<body>

<script src="/socket.io/socket.io.js"></script> 
<script type="text/javascript"> 
    var socket = io(); 
    socket.on('news', function(data){ 
     var obj= JSON.parse(data); 
     $("#json").text(obj); 
    }); 
</script> 

这运行正常,我有一个段落<p id="json"></p>这需要文本的整个块,但如果我尝试访问解析的JSON的一个元素并打印它,例如“时间戳”标签使用console.log(obj.timestamp)我得到undefined

如何处理我收到的数据,以便我可以将其作为常规JSON处理?

编辑

这是console.log(obj)输出:

{ 
"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 " 
        } 
       } 
      ] 
     } 
    } 
] 
} 

其中,根据JSON林特,是一个有效的JSON,但我依然得到undefined,当我尝试访问“时间戳”标记

回答

0

我会发送错误的数据格式,正确的代码是:

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"); 
    }); 
}); 
+0

也许你应该接受你自己的答案,如果它的工作;) – dgiugg 2014-08-27 11:43:01

+0

我试过了,但它说我需要回来在2天内接受它。我会做;然后;) – 2014-08-27 11:53:38

0

客户端和服务器之间应该没有关系。事实上,你使用node.js碰巧是JavaScript应该与客户端无关。

如果您在C#或Java中执行了相同的操作,对客户端会有什么影响吗?

底线,第1步应验证您的JSON对象在客户端上正确接收。然后,确保您的JSON是使用验证程序(如JSON Lint)的有效对象。如果它无效,那么问题在服务器端。如果没有,你需要检查你的客户端的解析。

+0

我打电话'JSON.parse'在双方检查是我处理的数据是正确的,我没有得到任何错误。我也在JSON Lint中复制粘贴'console.log(obj)'的输出,它说这是一个有效的json – 2014-08-27 10:51:57