Speed up JSON objects
I'm using the org.json.* library for turning my web services' result (obviously json) into json objects. My problem is that the JSONObject and JSONArray constructors take a long time to build out the objects. I'm not passing a very large amount of data (anywhere between 1 and 100 array items with 3-5 keys each), but even with just 4 or 5 it takes a few seconds for the constructor to finish.
Is there a way to speed t开发者_运维百科his up? Is there a faster library I could be using?
There's not a whole lot of code to show.
JSONArray arrayjson = new JSONArray(json);
Where json is a String.
You might give gson a try. This article seems to indicate that it has pretty good performance on Android vs the other alternatives. Jackson might be another good alternative.
According to the performance results at https://github.com/eishay/jvm-serializers/wiki, for serialization with databind with strings, e.g., gson.toJson(myObject), Gson is over 10X slower than Jackson. FastJSON beat Jackson at this same test by 2-3%.
Deserialization performance is similar, with Gson over 9x slower than Jackson, and FastJSON about 0.5% faster than Jackson.
Note: The current test results used Gson 1.6. With Gson 1.7.1, the databind performance improved 10-15% over Gson 1.6 (but the manual and manual/tree solutions showed no improvement). The results will hopefully be updated accordingly soon.
精彩评论