使用哪一种?来自 org.json 的 JSONObject 与来自 javax.json 的 JsonObject

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29042622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 07:14:39  来源:igfitidea点击:

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

javaandroidjson

提问by Phyxius81

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 SO.

这是我的第一篇文章。作为一个崭露头角的 Android 开发人员,我几乎每天都会阅读有关各种主题的 SO 帖子,但是对于这个问题,我没有从 Google 或 SO 找到任何帮助。

My research so far:
Searching for this question was harder than normal because the search engines don't seem to care about case-sensitivity, which is vital in this issue. Searching Google only gave me links to the classes themselves, old articles, or completely irrelevant articles. The closest I got was JSONArray vs JSONObject, and that is a completely different question. SO searching gave the same results. As far as I can tell, ALL pertinent posts on SO refer to JSONObject and not JsonObject.

到目前为止我的研究:
搜索这个问题比平常更难,因为搜索引擎似乎并不关心区分大小写,这在这个问题中至关重要。搜索谷歌只给我链接到课程本身、旧文章或完全不相关的文章。我得到的最接近的是 JSONArray 与 JSONObject,这是一个完全不同的问题。所以搜索给出了相同的结果。据我所知,SO 上的所有相关帖子都指的是 JSONObject 而不是 JsonObject。

Neither Oracle nor json.org documentation mentioned the other side, nor did the Android developer JSONObject page which uses the org.json library.

Oracle 和 json.org 文档都没有提到另一方,Android 开发人员 JSONObject 页面也没有提到使用 org.json 库的页面。

http://docs.oracle.com/javaee/7/api/javax/json/JsonObject.html
http://www.json.org/javadoc/org/json/JSONObject.html
I would have also posted a link to the Android reference page for JSONObject, but my rep as a newbie is too low.

http://docs.oracle.com/javaee/7/api/javax/json/JsonObject.html
http://www.json.org/javadoc/org/json/JSONObject.html
我也会发布一个链接到JSONObject 的 Android 参考页面,但我作为新手的代表太低了。

History of problem(s): I was reading about Json encoding and decoding from the Oracle page, and I tried copying and pasting into my AndriodStudio IDE, which immediately produced an error, for which it did not suggest an import statement like it normally does. I have come to learn that means the class is not part of the built-in libraries.

问题历史:我正在从 Oracle 页面阅读有关 Json 编码和解码的信息,我尝试复制并粘贴到我的 AndriodStudio IDE 中,这立即产生了一个错误,它没有像通常那样建议导入语句. 我了解到这意味着该类不是内置库的一部分。

The problem was that the code I pasted in used JsonObject (which comes from javax.json library) and not JSONObject (which comes from org.json library). When I noticed the difference in case between the Android page and the Oracle page, I altered my code from Json to JSON and immediately my IDE offered the org.json import statement.

问题是我粘贴的代码使用了 JsonObject(来自 javax.json 库)而不是 JSONObject(来自 org.json 库)。当我注意到 Android 页面和 Oracle 页面之间的大小写差异时,我将代码从 Json 更改为 JSON,并且我的 IDE 立即提供了 org.json 导入语句。

Question(s): Why is an Oracle made class not part of the default libraries when an external library is?
Is JSON the old, deprecated way and Json the new, proper way?
Are they functionally identical? If not, please provide examples?
Are some situations better for one and some better for the other?
ULTIMATELY, which should I use, and why (if not already covered)?
If, javax.json, where is the best (safest) place to download that library)?

问题:当外部库是默认库时,为什么 Oracle 生成的类不是默认库的一部分?
JSON 是旧的、已弃用的方式,而 Json 是新的、正确的方式吗?
它们在功能上是否相同?如果不是,请举例说明?
有些情况对一种情况更好,有些情况对另一种情况更好吗?
最终,我应该使用哪个,为什么(如果尚未涵盖)?
如果,javax.json,下载该库的最佳(最安全)位置在哪里)?

采纳答案by Samuel Méndez

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.

我最近遇到了这个问题,当我发现一个同时使用这两个库的 Java 项目时。

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

我认为这org.json更易于阅读和使用,主要有两个原因(根据我的需要):

  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();
    
  1. JsonObject 是不可变的。您不能向已经存在的 JsonObject 添加新的键/值对(参考此处:javax.json: Add new JsonNumber to existing JsonObject

  2. 漂亮地打印 JsonObject 或 JsonArray 需要几行,而使用 JSONObject 或 JSONArray 只需要 1 行。例子:

    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).

这是我用来获取缩进 JSON 以写入文件的代码。使用 org.json 我只需要jsonObject.toString(4).

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

另一个区别是构造函数。您将需要JsonObjectBuilder使用javax.json. 可以避免的多一步。

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

我确定有更多的差异(不确定是否可以JsonObject从字符串创建一个 ),但这些是我的想法。

回答by michip96

I recommend you to use Google's json-simpletoolkit 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.

我建议您使用 Google 的json-simple工具包来处理 JSON 对象。它提供了有用的功能,并且不依赖外部库,这在 Android 应用程序的开发过程中被证明是一个巨大的问题。

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.

回答你的问题:如果你不想在你的项目中使用 json-simple,你应该使用 org.json 中的 JSONObject,因为它是由 Android JDK 提供的。有关更多信息,请参阅http://developer.android.com/reference/org/json/JSONObject.html

回答by Varma

  1. org.jsondoes not contain a parse class for parsing the JSON string. whileorg.json.simplecontains a parse class.

  2. org.json.JSONObjectcontains lots of getter methods to get directly as int, boolean, object, string, JSON Array, etc. org.json.simple.JSONObjectdoes 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.JSONObjectfor parsing alone and type cast it into org.json.JSONObjectfor further use inside the program.

  1. org.json不包含用于解析 JSON 字符串的解析类。whileorg.json.simple包含一个解析类。

  2. org.json.JSONObject包含很多 getter 方法可以直接获取为 int、boolean、object、string、JSON Array 等。org.json.simple.JSONObject不包含特定的方法,因此每次获取特定键的值时都需要进行类型转换。

  3. 简单来说,org.json.simple.JSONObject用于单独解析,然后将其类型转换org.json.JSONObject为在程序中进一步使用。

回答by Full Stack

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.

如前所述,JSONObject 由 android 的 API 提供。JsonObject 专门用于 Java EE 开发,它主要用于 Web 应用程序和网络功能等。

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.

Android 没有从 Oracle Java EE 包中预先打包 JsonObject 的原因是因为 javax 可以做的很多事情在 android 中是不允许的,比如未经许可访问互联网。这意味着导入 javax 的整个 jars 文件会与 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.

如果您打算使用 Java EE 构建自己的后端,我强烈建议您使用 JsonObject 而不是 JSONObject。另一方面,如果您知道预构建的休息服务或类似的东西,它会更好地支持 Android 的 JSON。