英文:
Unable to access Array inside JSON object
问题
I am opening a websocket connection using WS v8+ 我正在使用 WS v8+ 打开一个 WebSocket 连接。
I receive a message and parse it to JSON. 我接收到一条消息并将其解析为 JSON。
Now the output is as expected: 现在输出如预期所示:
I can read the message.method and message.params as expected. The problem is accessing the array inside "params": 我可以按预期读取 message.method 和 message.params。问题在于访问 "params" 内部的数组:
I can't figure out why it is failing to see the params as an array with any length for this very simple task. 我弄不清楚为什么在这个非常简单的任务中它无法将 params 视为具有任何长度的数组。
UPDATE: 更新:
With the WS library, the need to use toString() from the Buffer was there. 使用 WS 库时,需要从 Buffer 中使用 toString()。
Also, the params field is not showing up as an array, which explains why length and other indexes aren't working, but I'm very confused as to why it isn't being read as an array with the output I am seeing. 此外,params 字段未显示为数组,这解释了为什么长度和其他索引不起作用,但我对它为什么没有被读取为数组感到非常困惑,因为我看到的输出是数组。
英文:
For the life of me, I can not figure out why this is not working.
I am opening a websocket connection using WS v8+
I receive a message and parse it to JSON.
ws.on('message', function message(data) {
var message = data.toString()
console.log(message)
});
Now the output is as expected:
{"method": "state.update", "params": [{"BTCUSDT": {"last": "27427.41"}}], "id": null}
I can read the message.method and message.params as expected. The problem is accessing the array inside "params":
ws.on('message', function message(data) {
var message = data.toString()
console.log(data.params[0]) // FAILS
});
ws.on('message', function message(data) {
var message = data.toString()
console.log(data.params)
// OUTPUT: [{"BTCUSDT": {"last": "27427.41"}}]
});
For the life of me I can't figure out why it is failing to see the params as an array with any length for this very simple task.
UPDATE:
With the WS library, the need to use toString() from the Buffer was there. With WS 7 this was not needed.
Also the params field is not showing up as an array which explains why length and other indexes aren't working but very confused as why it isn't being read as an array with the output I am seeing.
ws.on('message', function message(data) {
var message = data.toString()
var output = JSON.parse(message)
console.log( Array.isArray(output.params) ) // RETURNS FALSE
});
答案1
得分: 1
Assuming you're using this ws
library, it appears you're assuming the data is in JSON format, but I can't find any documentation to support that assumption.
在假设您正在使用此ws
库时,似乎您假设数据以JSON格式存在,但我找不到任何文档支持这一假设。
Try calling JSON.parse(data)
before iterating your object.
尝试在迭代对象之前调用 JSON.parse(data)
。
ws.on('message', function message(data) {
var json = JSON.parse(data) // <----- HERE
console.log(json.params[0])
});
英文:
Assuming you're using this ws
library, it appears you're assuming the data is in JSON format, but I can't find any documentation to support that assumption.
Try calling JSON.parse(data)
before iterating your object.
ws.on('message', function message(data) {
var json = JSON.parse(data) // <----- HERE
console.log(json.params[0])
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论