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

Node.js: handle JSON object in client side

本文关键字:处理 JSON 对象 客户端 js Node      更新时间:2023-09-26

在使用socket.ioNode.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");
    });
});

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

现在,在客户端,我在html的<body>:中有以下<script>

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

根据JSONLint的说法,它是一个有效的JSON,但当我尝试访问"时间戳"标签

时,我仍然得到undefined

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

如果您在C#或Java中执行了相同的操作,这对客户端有意义吗?

最重要的是,步骤1应该是验证客户端上是否正确接收到JSON对象。然后,使用JSON Lint等验证器确保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");
    });
});