Which one to use? JSONObject from org.json VS JsonObject from javax.json

后端 未结 4 1947
傲寒
傲寒 2020-12-28 12:45

This is my first post. As a budding Android developer, I read SO posts on a near daily basis on various topics, but for this question, I didn\'t find any help from Google or

相关标签:
4条回答
  • 2020-12-28 13:06
    1. org.json does not contain a parse class for parsing the JSON string. whileorg.json.simple contains a parse class.

    2. org.json.JSONObject contains lots of getter methods to get directly as int, boolean, object, string, JSON Array, etc. org.json.simple.JSONObject does not contain specific methods, so you need to type cast every time while getting value of a particular key.

    3. In simple words use org.json.simple.JSONObject for parsing alone and type cast it into org.json.JSONObject for further use inside the program.

    0 讨论(0)
  • 2020-12-28 13:13

    I recommend you to use Google's json-simple toolkit in order to work with JSON Objects. It provides useful functions and has no dependency on external libraries which turned out to be a huge problem during the development of Android apps.

    To answer your question: If you don't want to use json-simple in you project you should use the JSONObject from org.json, because it's provided by the Android JDK. For further information see http://developer.android.com/reference/org/json/JSONObject.html.

    0 讨论(0)
  • 2020-12-28 13:20

    JSONObject, as mentioned, is provided by android's API. JsonObject is specifically used for Java EE development which is essentially for web applications and networking capabilities among other things.

    The reason Android does not prepackage JsonObject from Oracle Java EE package is because alot of the things javax can do, are not allowed within android like accessing the internet without permission. This means importing the entire jars files of javax would conflict with Android.

    If you plan to build your own backend with Java EE, I would highly suggest using JsonObject over JSONObject. On the other hand, if you know a prebuilt rest service or something similar that supports Android's JSON even better.

    0 讨论(0)
  • 2020-12-28 13:32

    Bit late, but I wanted to share my opinion on this.

    I faced this problem recently when I found a Java project with both libraries and they were used at the same time.

    I think that org.json is easier to read and to use, for 2 main reasons (for my needs):

    1. JsonObject is immutable. You can't add new key/value pairs to an already existing JsonObject (reference here: javax.json: Add new JsonNumber to existing JsonObject)

    2. It takes a few lines to pretty print a JsonObject or JsonArray, while it only takes 1 line to do it with JSONObject or JSONArray. Example:

      StringWriter sw = new StringWriter();
      Map<String, Object> properties = new HashMap<>();
      properties.put(JsonGenerator.PRETTY_PRINTING, true);
      
      JsonWriterFactory writerFactory = Json.createWriterFactory(properties);
      JsonWriter jsonWriter = writerFactory.createWriter(sw);
      
      jsonWriter.writeObject(jsonObject); //JsonObject created before
      jsonWriter.close();
      String prettyPrintedJSON = sw.toString();
      

    That is the code I use to get an indented JSON to write to a file. And with org.json I only need jsonObject.toString(4).

    Another difference is the constructors. You will need a JsonObjectBuilder to create a JSON with javax.json. One step more that can be avoided.

    I'm sure there are more differences (not sure if it's possible to create a JsonObject from a String) but these are my thoughts.

    0 讨论(0)
提交回复
热议问题