Java 为什么在 JSON 字符串中将 double 转换为 int
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21965828/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why double is converted to int in JSON string
提问by Keerthivasan
I just coded to put an array of double values in the JsonObject
. But, all my double values are converted to int values, when i print it. can someone help me understand what is happening behind? Please let me know the best way to put primitive arrays in JsonObject
我只是编码以在JsonObject
. 但是,当我打印它时,我所有的 double 值都转换为 int 值。有人可以帮助我了解背后发生了什么吗?请让我知道将原始数组放入的最佳方法JsonObject
public class JsonPrimitiveArrays {
public static void main(String[] args) {
JSONObject jsonObject = new JSONObject();
double[] d = new double[]{1.0,2.0,3.0};
jsonObject.put("doubles",d);
System.out.println(jsonObject);
}
}
Output:
输出:
{"doubles":[1,2,3]}
{“双打”:[1,2,3]}
采纳答案by sakura
Its in real not getting converted into int. Only thing happening is as JS Object its not showing .0
which is not relevant.
它实际上没有转换为int。唯一发生的事情是作为 JS 对象,它没有显示.0
哪个不相关。
In your sample program, change some of the value from double[] d = new double[]{1.0,2.0,3.0}
to
在您的示例程序中,将一些值从 更改double[] d = new double[]{1.0,2.0,3.0}
为
double[] d = new double[]{1.0,2.1,3.1}
and run the program.
double[] d = new double[]{1.0,2.1,3.1}
并运行程序。
You will observer its in real not converting into int. The output you will get is {"doubles":[1,2.1,3.1]}
您将实际观察它而不是转换为 int。你会得到的输出是{"doubles":[1,2.1,3.1]}
回答by Teddy
All numbers are floats in Javascript. So, 1.0 and 1 are the same in JS. There is no differenciation of int, float and double.
所有数字都是 Javascript 中的浮点数。所以,1.0 和 1 在 JS 中是一样的。int、float 和 double 没有区别。
Since JSON is going to end up as a JS object, there is no use in adding an extra '.0' since '1' represents a float as well. I guess this is done to save a few bytes in the string that is passed around.
由于 JSON 最终会成为 JS 对象,因此添加额外的 '.0' 没有用,因为 '1' 也代表一个浮点数。我想这样做是为了在传递的字符串中保存几个字节。
So, you will get a float in JS, and if you parse it back to Java, you should get a double. Try it.
所以,你会在 JS 中得到一个浮点数,如果你把它解析回 Java,你应该得到一个双精度值。尝试一下。
In the mean time, if you are interested in the way it displays on screen, you can try some string formatting to make it look like '1.0'.
同时,如果您对它在屏幕上的显示方式感兴趣,可以尝试一些字符串格式,使其看起来像“1.0”。
回答by PopoFibo
Looking at toString
of net.sf.json.JSONObject
it eventually calls the following method to translate the numbers to String
(source code here):
看toString
的net.sf.json.JSONObject
是最终调用下面的方法翻译的号码String
(这里的源代码):
public static String numberToString(Number n) {
if (n == null) {
throw new JSONException("Null pointer");
}
//testValidity(n);
// Shave off trailing zeros and decimal point, if possible.
String s = n.toString();
if (s.indexOf('.') > 0 && s.indexOf('e') < 0 && s.indexOf('E') < 0) {
while (s.endsWith("0")) {
s = s.substring(0, s.length() - 1);
}
if (s.endsWith(".")) {
s = s.substring(0, s.length() - 1);
}
}
return s;
}
It clearly tries to get rid of the trailing zeroes when it can, (s = s.substring(0, s.length() - 1)
if a String is ending in zero).
它显然试图在可能的情况下摆脱尾随零,(s = s.substring(0, s.length() - 1)
如果字符串以零结尾)。
System.out.println(numberToString(1.1) + " vs " + numberToString(1.0));
Gives,
给,
1.1 vs 1