I am not able to understand the difference between JSONObject and GSON for parsing JSON strings in Android.
Can somebody explain?
JSON. simple is a Java toolkit for encoding and decoding JSON text. It's meant to be a lightweight and simple library that still performs at a high level. GSON is a Java library that converts Java Objects into JSON and vice versa.
This isnt base Java, those are Objects depending on the JSON-libary you are using. JSONObject is "native" to Android SDK, JsonObject is probably the one from Gson library, the one that I use. Two different package, don't work with both ;) choose one.
1. Jackson. Jackson is a multi-purpose Java library for processing JSON data format. Jackson aims to be the best possible combination of fast, correct, lightweight, and ergonomic for developers.
Last benchmarks Jackson was the winner, but this time GSON is by far the fastest, with JSONP being a close second and then followed by Jackson and then JSON. simple last. And again looking at the numbers from two different angles, here are the percentage results: GSON is a clear winner.
GSON can use the Object definition to directly create an object of the desired type. JSONObject needs to be parsed manually. Thats the main difference.
as the others said, GSON
should make mapping JSON
to POJO
much easier, BUT, the latest GSON
version (as I write, 2.2.2) has serious issues with the latest Android API 17 (Android 4.2).
Check this for information: http://code.google.com/p/google-gson/issues/detail?id=496
If you are doing a new project now, and want it to be compatible with Android 4.2, either use an older GSON
(1.7.1 works fine with Android 4.2, but is slower), or find another solution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With