0
0
Android-kotlinHow-ToBeginner ยท 3 min read

How to Parse JSON in Android: Simple Guide with Example

In Android, you can parse JSON using the JSONObject and JSONArray classes from the org.json package. First, convert your JSON string into a JSONObject, then extract values using keys or iterate arrays with JSONArray.
๐Ÿ“

Syntax

Use JSONObject to parse JSON objects and JSONArray for JSON arrays. You create a JSONObject by passing a JSON string to its constructor. Then use methods like getString(), getInt(), or getJSONArray() to access data.

java
String jsonString = "{\"name\":\"John\", \"age\":30, \"cars\":[\"Ford\", \"BMW\"]}";
JSONObject obj = new JSONObject(jsonString);
String name = obj.getString("name");
int age = obj.getInt("age");
JSONArray cars = obj.getJSONArray("cars");
String firstCar = cars.getString(0);
๐Ÿ’ป

Example

This example shows how to parse a JSON string representing a person with a name, age, and a list of cars. It extracts and prints these values.

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

public class JsonParseExample {
  public static void main(String[] args) {
    try {
      String jsonString = "{\"name\":\"John\", \"age\":30, \"cars\":[\"Ford\", \"BMW\"]}";
      JSONObject obj = new JSONObject(jsonString);
      String name = obj.getString("name");
      int age = obj.getInt("age");
      JSONArray cars = obj.getJSONArray("cars");

      System.out.println("Name: " + name);
      System.out.println("Age: " + age);
      System.out.print("Cars: ");
      for (int i = 0; i < cars.length(); i++) {
        System.out.print(cars.getString(i));
        if (i < cars.length() - 1) System.out.print(", ");
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}
Output
Name: John Age: 30 Cars: Ford, BMW
โš ๏ธ

Common Pitfalls

  • Not handling exceptions: JSON parsing throws JSONException, so always use try-catch.
  • Wrong key names: JSON keys are case-sensitive, so use exact names.
  • Assuming data types: Use correct getter methods matching the JSON data type.
  • Not checking for null or missing keys can cause crashes.
java
try {
  JSONObject obj = new JSONObject(jsonString);
  // Wrong key name - causes JSONException
  String wrong = obj.getString("Name");
} catch (Exception e) {
  e.printStackTrace();
}

// Correct way
try {
  JSONObject obj = new JSONObject(jsonString);
  String correct = obj.getString("name");
} catch (Exception e) {
  e.printStackTrace();
}
๐Ÿ“Š

Quick Reference

Here is a quick summary of common JSONObject and JSONArray methods:

MethodDescription
JSONObject(String json)Creates a JSONObject from a JSON string
getString(String key)Gets a string value by key
getInt(String key)Gets an integer value by key
getJSONArray(String key)Gets a JSONArray by key
JSONArray.length()Returns the number of items in the array
JSONArray.getString(int index)Gets a string at the given index
โœ…

Key Takeaways

Use JSONObject and JSONArray classes to parse JSON strings in Android.
Always handle JSONException with try-catch to avoid crashes.
Use exact key names and matching getter methods for correct data extraction.
Iterate JSONArray with a loop to access array elements.
Check for null or missing keys to make your app robust.