In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is to share with you about what you need to avoid when using JSONObject. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Problem phenomenon
In the logic code of Android business synchronization, JSONObject is used to parse the JSON data on the server side. At the same time, due to the new business demand, JSONObject is used in the local database to cache synchronization-related information, including water level, where the water level value is Long. However, it has recently been found that during the next synchronization, the water level passed to the server is not the new water level returned by the last server, but a difference. Take 301028292893495297L as an example, after the server returns this water level, the next time the client uploads the water level is 301028292893495296L, with a difference of-1.
Problem troubleshooting
By repeatedly checking the code logic, it is found that only the following conversion occurs between the return of the water level from the server side to the next request:
Reading the code carefully, it is not difficult to find that the water level value of Long type is converted to String type when saved in the JSON object, and is treated as Long type when reading. As a result, there will be a lack of precision, as shown in the following JSONObject documentation:
It can be seen that when reading a value of JSON object, if it was originally String, it will be parsed as Long when reading, and String will be parsed through Double, so there will be a lack of precision when the value exceeds 2 ^ 52. Therefore, the problems encountered can be explained. The following is the storage format specification for Double:
Among them, the precision test code of Double and Long is very simple (input parameters can provide long values exceeding 2 ^ 52, such as 301028292893495297L, and you will find that the return value is not 0):
The precision test code for Double and Long is simple (input parameters can provide long values exceeding 2 ^ 52, such as 301028292993495297L):
Once you know the root cause of the problem, the fix is clear. When the water level is stored in the JSONObject object, it should be saved as Long instead of String, or as String when reading, and then parsed through APIs such as Long.valueOf.
In addition, it is easier to ignore whether the values in JSON objects are of type Long or type String. If the JSON object is represented by String, the quotation marks corresponding to the value are String. Take a look at the following trial example at a glance:
Similar questions are randomly searched on the Internet, in fact, many people have been trapped, such as this one.
Therefore, although it cannot be said that the design of this library is a failure, it is certainly not a well-designed library. Because you can't see the underlying logic of API directly from its name, it can easily lead to improper use by users. Therefore, the lesson is: when using a third-party library, look at API documents as well as API documents, not literally. Of course, this problem may be limited to the older code modules in Android, after all, the new code will use GSON and other class libraries for JSON object manipulation, so it is not easy to find such a problem.
Of course, as far as this problem is concerned, when the new business logic is added, the interface of the JSONObject object is not used correctly, and the value of Long type should not be saved as String type and read as Long type. If the stored and read interfaces remain corresponding, there will be no problem. In any case, the lesson of this problem is to be careful when using JSONObject-related interfaces.
Note: the JSON-Java library on Github does not have this problem, so you can rest assured to use it.
Problem solving
Once you know the root cause of the problem, the fix is clear. When the water level is stored in the JSON object, it should be saved as Long instead of String, or as String when reading, and then parsed through interfaces such as Long.valueOf.
Thank you for reading! This is the end of this article on "what problems need to be avoided when using JSONObject". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.