【发布时间】:2010-11-22 13:06:27
【问题描述】:
在 SmartfoxServer(使用 Rhino)的服务器端扩展中,我有一段类似的 Javascript:
response["xpos"] = properties.get("xpos");
send(JSON.stringify(response));
这导致了错误。发生了什么?因为 properties 是一个 Java Map,所以当一个数字被放入其中时,它会自动装箱到一个 java.lang.Double 对象中。因此,当检索它并将其存储在response["xpos"] 中时,结果不是常规的 Javascript 数字,而是java.lang.Double 类型的JavaObject。 JSON.stringify 函数本来不能处理这个问题,结果它崩溃了。
我用这样的 hack 修复了它:
response["xpos"] = 1.0 * properties.get("xpos");
send(JSON.stringify(response));
有没有更好的办法?
【问题讨论】:
标签: java javascript rhino autoboxing