哪一个使用?来自org.json的JSONObject来自javax.json的VS JsonObject [英] Which one to use? JSONObject from org.json VS JsonObject from javax.json

查看:104
本文介绍了哪一个使用?来自org.json的JSONObject来自javax.json的VS JsonObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

到目前为止我的研究:

搜索这个问题比正常情况更难,因为搜索引擎似乎并不关心区分大小写,这在这个问题上至关重要。搜索Google只会给我链接到课程本身,旧文章或完全不相关的文章。我得到的最接近的是JSONArray和JSONObject,这是一个完全不同的问题。 SO搜索给出了相同的结果。据我所知,SO上的所有相关帖子都引用JSONObject而不是JsonObject。

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.

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

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

http://docs.oracle.com/javaee/7/api/javax/json/JsonObject.html

http://www.json.org/javadoc/org/json/JSONObject.html

我也会发布了一个指向JSONObject的Android参考页面的链接,但我作为新手的代表太低了。

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.

问题的历史:
我正在阅读关于Json编码和解码来自Oracle页面,我尝试复制并粘贴到我的AndriodStudio IDE中,这会立即产生错误,因为它没有像通常那样建议导入语句。我已经开始学习,这意味着该类不是内置库的一部分。

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.

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

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.

问题:
当外部库是Oracle时,为什么Oracle使类不属于默认库?

JSON是旧的,不赞成使用的方式,Json是新的,正确的方法吗?

它们功能相同吗?如果没有,请提供示例?

某些情况对一个情况有好处,有些情况对另一个情况有好处吗?

ULTIMATELY,我应该使用哪个,以及为什么(如果还没有涵盖)?

如果,javax.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)?

推荐答案

<有点迟了,但我想就此分享我的意见。

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

我最近遇到了这个问题,当时我发现了一个带有两个库的Java项目,并且它们被用于同时。

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

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

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


  1. JsonObject是不可变的。您不能将新的键/值对添加到现有的JsonObject中(参考此处: javax.json:将新的JsonNumber添加到现有的JsonObject中

需要几行才能打印出JsonObject或JsonArray,而使用JSONObject或JSONArray只需要1行。示例:

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();


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

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

另一个区别是构造函数。您将需要一个 JsonObjectBuilder 来创建一个带有 javax.json 的JSON。还有一步可以避免。

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

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

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.

这篇关于哪一个使用?来自org.json的JSONObject来自javax.json的VS JsonObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆