org.json.JSONObject.getLong gives imprecise result

2019-02-19 09:50发布

Wrong precision in Long conversion

In android, as we can see attached figure, the json that I received from server is "id=-7051563524465472446" but the code jsonObject.getLong("id") gives -7051563524465472512.

I can see every getLong conversion has imprecise result(or lose precision?) in last 4 digits. Is there anything that I missed?

My settings: java 1.6.0_41, Android 4.1.2, IntelliJ 12 IDE.

3条回答
迷人小祖宗
2楼-- · 2019-02-19 10:02

Long.parseLong(jsonObject.getString("id")) gives precise result.

查看更多
做个烂人
3楼-- · 2019-02-19 10:06

This would suggest the code for getLong() is doing some dubious conversion, possibly by using double.

This looks to be the case based on this issue report. Looks to have been fixed in a later version.

查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-02-19 10:12

I too faced this problem very recently.. Solved it by using the code:

Long.valueOf(jarray.getString((index)))
查看更多
登录 后发表回答