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.

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.


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



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


无法向 Json 数组添加新元素

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


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

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

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

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

如何在python中读取json对象 带有非常嵌套的 json

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

如何在notepad++ 中格式化JSON


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


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

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

使用电源查询的 Json excel

将 List> 序列化为 JSON