Java - Easiest way to parse JSON

An introduction to parsing JSON using the org.json library.

What is JSON?

JSON consists of Object, Array, and Key-Value types and supports types such as String, Int, Long, and Boolean.

Object

Object is something enclosed in { } (curly braces). For example, in the JSON code below, there is one object, and that object has four keys: title, url, draft, and star and corresponding values.

{
  "title": "how to get stroage size",
  "url": "https://codechacha.com/ko/get-free-and-total-size-of-volumes-in-android/",
  "draft": false,
  "star": 10
}

Key-Value

For Key-Value, taking the above title as an example, "title" is the key, and "how to get stroage size" after "how to get stroage size" is the value. The key must be enclosed in colons like "...". If the value is a String type, it is expressed as "...", and Boolean or Integer is just used.

"title": "how to get stroage size",

Array

An Array is something enclosed in [ ] (square bracket).

{
  "posts": [
      {
          "title": "how to get stroage size",
          "url": "https://codechacha.com/ko/get-free-and-total-size-of-volumes-in-android/",
          "draft": false
      },
      {
            "title": "Android Q, Scoped Storage",
            "url": "https://codechacha.com/ko/android-q-scoped-storage/",
            "draft": false
      },
      {
            "title": "How to parse JSON in android",
            "url": "https://codechacha.com/ko/how-to-parse-json-in-android/",
            "draft": true
      }
  ]
}

JSON library

A project using Gradle can add a JSON library like this:

dependencies {
    implementation 'org.json:json:20190722'
    ...
}

Please add other projects yourself.

Parse JSON

Here is an example of parsing the following 3 types of JSON.

  1. JSON with only simple key-value
  2. JSON with multiple Objects under it
  3. JSON with Array

1. JSON with simple key-value only

The JSON string to be parsed is hard-coded directly into the code.

import org.json.JSONArray;
import org.json.JSONObject;

public void jsonParsing1() {
    String jsonString = "{\"title\": \"how to get stroage size\","
            + "\"url\": \"https://codechacha.com/ko/get-free-and-total-size-of-volumes-in-android/\","
            + "\"draft\": false,"
            + "\"star\": 10"
            + "}";

    // Get JSONObjet and read key-value
    JSONObject jObject = new JSONObject(jsonString);
    String title = jObject.getString("title");
    String url = jObject.getString("url");
    Boolean draft = jObject.getBoolean("draft");
    int star = jObject.getInt("star");

    System.out.println("title: " + title);
    System.out.println("url: " + url);
    System.out.println("draft: " + draft);
    System.out.println("star: " + star);
}

result

title: how to get stroage size
url: https://codechacha.com/ko/get-free-and-total-size-of-volumes-in-android/
draft: false
star: 10

2. JSON with multiple Objects under it

The JSON string to be parsed is hard-coded directly into the code.

import org.json.JSONArray;
import org.json.JSONObject;

public void jsonParsing2() {
    String jsonString =
        "{"
        +   "\"post1\": {"
        +       "\"title\": \"how to get stroage size\","
        +       "\"url\": \"https://codechacha.com/ko/get-free-and-total-size-of-volumes-in-android/\","
        +       "\"draft\": false"
        +"  },"
        +   "\"post2\": {"
        +       "\"title\": \"Android Q, Scoped Storage\","
        +       "\"url\": \"https://codechacha.com/ko/android-q-scoped-storage/\","
        +       "\"draft\": false"
        +   "}"
        +"}";

    // Get the largest JSONObject
    JSONObject jObject = new JSONObject(jsonString);

    // Get the first JSONObject and read the key-value
    JSONObject post1Object = jObject.getJSONObject("post1");
    System.out.println(post1Object.toString());
    System.out.println();
    String title = post1Object.getString("title");
    String url = post1Object.getString("url");
    boolean draft = post1Object.getBoolean("draft");
    System.out.println("title(post1): " + title);
    System.out.println("url(post1): " + url);
    System.out.println("draft(post1): " + draft);
    System.out.println();

    // Get the second JSONObject and read the key-value
    JSONObject post2Object = jObject.getJSONObject("post2");
    System.out.println(post2Object.toString());
    System.out.println();
    title = post2Object.getString("title");
    url = post2Object.getString("url");
    draft = post2Object.getBoolean("draft");
    System.out.println("title(post1): " + title);
    System.out.println("url(post1): " + url);
    System.out.println("draft(post1): " + draft);
}

result

{"draft":false,"title":"how to get stroage size","url":"https://codechacha.com/ko/get-free-and-total-size-of-volumes-in-android/"}

title(post1): how to get stroage size
url(post1): https://codechacha.com/ko/get-free-and-total-size-of-volumes-in-android/
draft(post1): false

{"draft":false,"title":"Android Q, Scoped Storage","url":"https://codechacha.com/ko/android-q-scoped-storage/"}

title(post1): Android Q, Scoped Storage
url(post1): https://codechacha.com/ko/android-q-scoped-storage/
draft(post1): false

3. JSON with Array

The JSON string to be parsed is hard-coded directly into the code.

import org.json.JSONArray;
import org.json.JSONObject;

public void jsonParsing3() {
    String jsonString =
    "{"
    +   "\"posts\": ["
    +       "{"
    +           "\"title\": \"how to get stroage size\","
    +           "\"url\": \"https://codechacha.com/ko/get-free-and-total-size-of-volumes-in-android/\","
    +           "\"draft\": false"
    +       "},"
    +       "{"
    +           "\"title\": \"Android Q, Scoped Storage\","
    +           "\"url\": \"https://codechacha.com/ko/android-q-scoped-storage/\","
    +           "\"draft\": false"
    +       "},"
    +       "{"
    +           "\"title\": \"How to parse JSON in android\","
    +           "\"url\": \"https://codechacha.com/ko/how-to-parse-json-in-android/\","
    +           "\"draft\": true"
    +       "}"
    +   "]"
    +"}";

    // Get the largest JSONObject
    JSONObject jObject = new JSONObject(jsonString);
    // get an array
    JSONArray jArray = jObject.getJSONArray("posts");

    // Print all items in the array
    for (int i = 0; i < jArray.length(); i++) {
        JSONObject obj = jArray.getJSONObject(i);
        String title = obj.getString("title");
        String url = obj.getString("url");
        boolean draft = obj.getBoolean("draft");
        System.out.println("title(" + i + "): " + title);
        System.out.println("url(" + i + "): " + url);
        System.out.println("draft(" + i + "): " + draft);
        System.out.println();
    }
}

result

title(0): how to get stroage size
url(0): https://codechacha.com/ko/get-free-and-total-size-of-volumes-in-android/
draft(0): false

title(1): Android Q, Scoped Storage
url(1): https://codechacha.com/ko/android-q-scoped-storage/
draft(1): false

title(2): How to parse JSON in android
url(2): https://codechacha.com/ko/how-to-parse-json-in-android/
draft(2): true
codechachaCopyright ©2019 codechacha