【发布时间】:2017-11-22 22:51:27
【问题描述】:
我正在编写一个程序来将包含一个属性中的纪元时间的 JSON 内容转换为 XML 格式并将其存储在一个字符串中。
下面是代码
String data = "{\"result\":\"pass\", \"timestamp\":1497870880.0396869183}";
JSONObject json = new JSONObject(data); //the json object contains proper epoch value after this line
String event = XML.toString(json);
当我打印我得到的字符串事件的内容时
<result>pass</result><timestamp>1.497870880039687E9</timestamp>
timestamp的值改变了。
但如果 JSON 内容具有纪元时间戳作为字符串,例如
String data = "{\"result\":\"pass\", \"timestamp\":"1497870880.0396869183"}";
然后生成的事件字符串给出正确的结果
<result>pass</result><timestamp>1497870880.0396869183</timestamp>
谁能解释一下为什么会这样? JSON XML API 对 LONG 值不友好吗? 注意:输入的 JSON 内容将包含具有字符串值和纪元时间的多个属性。
【问题讨论】:
-
价值并没有完全改变。它正在变为科学计数法
-
在 XML 之前打印出 JSONObject。问题可能是json库
-
json 对象具有正确和准确的纪元值和准确的内容作为 json 内容。问题发生在 toString() 调用之后。