在node.js和socket.io中发送数组时的错误

发布于 2024-12-27 12:35:45 字数 832 浏览 1 评论 0原文

我使用 socket.io 版本 0.8.4

我已将问题归结为以下内容。我的数据如下所示:

data.prop1 = [];
data.prop1.push("man");
data.prop2 = [];
data.prop2["hey"] = "man";

我以这种方式将数据从服务器发送到客户端:

socket.emit("data", data);

在客户端,我以这种方式接收数据:

socket.on("data", function(data){ console.log(data); });

奇怪的是:

data.prop1 = [];
data.prop1.push("man"); // This data exists in the client side data object
data.prop2 = [];
data.prop2["hey"] = "man"; // This data does not exist.

data.prop2 在客户端只是一个空数组。

prop2 中表单上的 json 序列化数组是否存在已知错误?

提前谢谢

您编辑:

使用此解决方法解决了问题:

data.prop1 = [];
data.prop1.push("man");
data.prop2 = {}; // <= Object instead of array
data.prop2["hey"] = "man";

I use socket.io version 0.8.4

I have boiled down my problem to the following. I have data looking like this:

data.prop1 = [];
data.prop1.push("man");
data.prop2 = [];
data.prop2["hey"] = "man";

I send the data from the server to the client this way:

socket.emit("data", data);

On the client side I receive the data this way:

socket.on("data", function(data){ console.log(data); });

The weird thing is:

data.prop1 = [];
data.prop1.push("man"); // This data exists in the client side data object
data.prop2 = [];
data.prop2["hey"] = "man"; // This data does not exist.

data.prop2 is just an empty array on the client side.

Is there a known bug in json serializing arrays on the form in prop2?

Thankyou in advance

EDIT:

Problem solved using this workaround:

data.prop1 = [];
data.prop1.push("man");
data.prop2 = {}; // <= Object instead of array
data.prop2["hey"] = "man";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

三人与歌 2025-01-03 12:35:45

关于 JSON.stringify 的 ECMA-262:

数组的表示仅包含零到array.length – 1(含)之间的元素。命名属性从字符串化中排除。

数组应该有数字属性名称。因此,当data.prop2转换为JSON(我想socket.io发送数据)时,它不会获得'hey'属性。如果你想使用非数字属性名称,你应该使用对象而不是数组:

data.prop1 = [];
data.prop1.push("man");
data.prop2 = {}; // Notice we're creating an object, not an array.
data.prop2["hey"] = "man"; // Alternatively: data.prop2.hey = "man"

ECMA-262 about JSON.stringify:

The representation of arrays includes only the elements between zero and array.length – 1 inclusive. Named properties are excluded from the stringification.

Arrays are supposed to have numerical property names. So when the data.prop2 is transformed to JSON (which socket.io sends the data in, I imagine), it doesn't get the 'hey' property. If you want to use non-numerical property names, you should use objects instead of arrays:

data.prop1 = [];
data.prop1.push("man");
data.prop2 = {}; // Notice we're creating an object, not an array.
data.prop2["hey"] = "man"; // Alternatively: data.prop2.hey = "man"
蓝颜夕 2025-01-03 12:35:45

不幸的是,Javascript 并不是这样工作的。

查看这篇文章,大约一半。它解释了您尝试设置 data.prop2["hey"] = "man"; 的问题

Unfortunately, Javascript doesn't really work like that.

Check out this article, about half way down. It explains the problem where you try to set data.prop2["hey"] = "man";

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文