GWT 的 JSONParser 生成错误的数字值
我正在使用 GWT 并解析来自返回 DataTable 的 ASP.NET Web 服务方法的 JSON 结果。我可以将结果解析为 JSONvalue/JSONObject 。我遇到的问题是 DECIMAL(20, 0) 中的一列和解析为 JSON 的值不准确。为了演示不需要 WS 调用,在 GWT 中我将其放在一起:
String jsonString = "{value:4768428229311981600}";
JSONObject jsonObject = JSONParser.parse( jsonString ).isObject();
Window.alert( jsonObject.toString() );
这反过来又提醒:
{"value":4768428229311982000}
我理解 GWT 的 JSONParser 只是使用 eval() 进行解析,所以这是某种我从未意识到 JavaScript 的数字/精度问题。我承认我在 JavaScript 中处理数字的次数不多,我也许可以通过更改 .NET WebService 以将此列作为字符串返回来解决此问题,但我真的不想这样做。
感谢您的任何帮助。
I'm work with GWT and parsing a JSON result from an ASP.NET webservice method that returns a DataTable. I can parse the result into a JSONvalue/JSONObject just fine. The issue I'm having is that one my columns in a DECIMAL(20, 0) and the values that are getting parsed into JSON aren't exact. To demonstrate w/o the need for a WS call, in GWT I threw this together:
String jsonString = "{value:4768428229311981600}";
JSONObject jsonObject = JSONParser.parse( jsonString ).isObject();
Window.alert( jsonObject.toString() );
This in turn alerts:
{"value":4768428229311982000}
I'm under the understanding that GWT's JSONParser is just using eval() to do the parsing, so is this some sort of number/precision issue with JavaScript that I've never been aware of. I'll admit I don't work with numbers that much in JavaScript and I might be able to work around this by changing the .NET WebService to return this column as string, but I'd really rather not do that.
Thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我前段时间回答过一个类似的问题 - GWT中的任意精度
一个更新的答案是 GWT 2.1 的 BigDecimal 支持看起来已经步入正轨,
在那之前,如果您不需要在客户端对数字进行计算,我建议将它们作为字符串传递。
此外,看看您的示例,您可以将它们作为字符串传输,并且可能使用模拟的 GWT java.lang.Long。
最后,您可以尝试 svn 版本的 BigDecimal GWT -Math - 将 java 文件放入你的 jar 中应该不会那么糟糕(它不需要编译,因为它都是 emul 代码)
如果你走这条路,你仍然需要通过数字作为 JSON 字符串,但您可以执行有意义的数学运算。
There was a similar question I answered sometime ago - Arbitrary precision in GWT
A more up to date answer is that BigDecimal support looks on track for GWT 2.1
Until then, if you don't need to do calculation with the numbers client side, I recommend passing them around as strings.
Additionally, looking at your example, you could transfer them as strings and maybe use the emulated GWT java.lang.Long.
Last ditch, you can try the svn version of BigDecimal GWT-Math - it shouldn't be all that bad to drop the java files into your jar (It would not need to be compiled, since it's all emul code)
If you go that route, you would still have to pass the numbers as JSON strings, but you could perform meaningful math.
好吧,Javascript 仅使用普通的 IEEE 754 64 位浮点,因此存在固有的精度限制。该语言不支持任意大小的整数(或者实际上根本不支持任何纯整数)。当您需要操作 Javascript 中的值时,您将必须使用字符串表示形式,并且希望您不必进行任何数学运算。
编辑:我之前看过这个:http ://www-cs-students.stanford.edu/~tjw/jsbn/
如果您不需要对数字进行太多操作,这似乎是一个相当复杂的解决方案,但可能值得一看。这个想法可能还有一些不那么雄心勃勃的变化。
无论如何,这不会帮助您直接解释 JSON,除非您还连接了一个变体 JSON 解析器来使用此类库构造数值。
Well, Javascript just uses ordinary IEEE 754 64-bit floating point, so there's an inherent precision limit. The language does not provide support for arbitrary-sized integers (or, really, any pure integer at all). You're going to have to use a string representation when you need to manipulate the values in Javascript, and hopefully you won't have to do any math.
edit: I've looked before at this: http://www-cs-students.stanford.edu/~tjw/jsbn/
It seems like a fairly hairy solution if you don't need to do much manipulating of the numbers, but it might be worth looking at. There may be less ambitious variations on that idea out there.
In any case, that's not going to help you with straight interpretation of JSON unless you also wired up a variant JSON parser to construct numeric values using such a library.