Node.js:处理客户端的JSON对象

时间:2014-08-27 08:32:00

标签: javascript json node.js

我无法使用JSONNode.js服务器的客户端处理socket.io对象。

这就是我发送数据的方式(我正在阅读的文件有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");
    });
});

我致电JSON.stringify,以便为二进制文件data提供可读格式。

现在,在客户端,我在html的<script>中有以下<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)的“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 Lint的说法,它是一个有效的json,但当我尝试访问“timestamp”标签时,我仍然得到undefined

2 个答案:

答案 0 :(得分:0)

客户端和服务器之间应该没有关系。您使用恰好是JavaScript的node.js的事实应该与客户端无关。

如果您在C#或Java中执行了相同的操作,那么客户端是否重要?

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

答案 1 :(得分: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");
    });
});