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),但这些是我的想法.

Json相关问答推荐

从 json 对象中过滤掉所有出现的给定属性

序列化为json时如何忽略空列表?

无法向 Json 数组添加新元素

C# 合并 2 个几乎相同的 JSON 对象

在PowerShell中按时间戳过滤JSON

json对象的大小? (以 KB/MB 为单位)

错误:List 不是 Map 类型的子类型

MySQL 选择 JSON 字段属性具有值的位置

对 kotlin 数据类使用 Jackson @JsonProperty 注释

如何在python中读取json对象

pandas.io.json.json_normalize 带有非常嵌套的 json

Swift 4 可解码,直到解码时才知道密钥

如何在notepad++ 中格式化JSON

没有从字符串值('')反序列化的字符串参数构造函数/工厂方法

Swift 4 可编码;如何使用单个根级密钥解码对象

杰克逊数据绑定枚举不区分大小写

使用 jq 就地修改 json 中的键值

新的@angular/cli 版本中的文件 angular-cli.json 在哪里?

使用电源查询的 Json excel

将 List> 序列化为 JSON