org.json.JSONObject vs Gson library JsonObject

iamcrypticcoder picture iamcrypticcoder · Mar 7, 2017 · Viewed 23.6k times · Source

What are differences between this two classes?

If someone uses Gson library is it preferable to use com.google.json.JsonObject over org.json.JSONObject?

Could anybody list pros and cons of these 2 choices?

Answer

Mehmood Memon picture Mehmood Memon · Mar 7, 2017

Following are the main differences:

1) GSON can use the Object definition to directly create an object of the desired type. JSONObject needs to be parsed manually.

2) org.json is a simple, tree-style API. It's biggest weakness is that it requires you to load the entire JSON document into a string before you can parse it. For large JSON documents this may be inefficient.

3) By far the biggest weakness of the org.json implementation is JSONException. It's just not convenient to have to place a try/catch block around all of your JSON stuff.

4) Gson is the best API for JSON parsing on Android. It has a very small binary size (under 200 KiB), does fast databinding, and has a simple easy-to-use API.

5) GSON and Jackson are the most popular solutions for managing JSON data in the java world.