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.

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文档都没有提到另一个方面,使用org.json库的Android开发人员JSONObject页面也没有提到.

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.

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.

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.

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 think that org.json is easier to read and to use, for 2 main reasons (for my needs):

  1. JsonObject是不可变的.不能将新的键/值对添加到已经存在的JsonObject(此处参考:javax.json: Add new JsonNumber to existing JsonObject)

  2. 漂亮地打印JsonObject或JsonArray只需要几行代码,而使用JSONObject或JSONArray只需要一行代码.示例:

    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.

我相信还有更多不同之处(不确定是否有可能从一个字符串创建JsonObject),但这些是我的 idea .

Json相关问答推荐

您可以使用Jolt对JSON执行OR条件吗

需要有关在Ffltter应用程序中解码JSON的帮助;未处理的异常:类型不是类型转换中类型的子类型

使用JQ从jsonl文件中删除具有匹配键/值的行

来自json的可分析的构建报告

NoneType 对象的 Python 类型错误

Jolt 变换 - 如何用字段值重命名字段?

添加到数组时出错:找不到Add的重载和参数计数:1

JOLT JSON 将值从一对多转换为一对一

Powershell 7.2:ConvertFrom-Json - 日期处理

N1QL 聚合查询 Couchbase

将 JSON 数据导入 Google 表格

Python - 如何将 JSON 文件转换为数据框

Jackson Scala 模块的小例子?

Rails 中奇怪的 JSON Javascript 问题

将 JSON 对象推送到 localStorage 中的数组

C#扁平化json struct

如何使用 Serde 反序列化带有自定义函数的可选字段?

将 json 转换为 C# 数组?

带有 Jackson 的不可变 Lombok 注释类

MVC JsonResult camelCase 序列化