Tutorials  /  JavaScript

Mastering JSON Parsing in Android with JSONObject: A Tutorial

Ccentron Redaktion · September 2024 ·3 min read ·JavaScript, Tutorial

In the realm of Android development, JSON (JavaScript Object Notation) stands as an essential component for data interchange between client and server. JSON offers a simple and efficient way to transmit and process structured data. In this tutorial, we will delve into parsing JSON in Android applications, particularly using JSONObject.

What is JSON?

JSON is a human-readable text-based data interchange format, particularly suitable for web applications. Unlike XML, JSON is easier to understand and less redundant. It is language-independent and can be utilized in various programming languages such as Java, C, and C++.

A typical JSON structure comprises key-value pairs, with values presented in the form of objects or arrays. Below is an example of a JSON response from a server:

JSON
{
  "title": "JSONParserTutorial",
  "array": [
    {"company": "Google"},
    {"company": "Facebook"},
    {"company": "LinkedIn"},
    {"company": "Microsoft"},
    {"company": "Apple"}
  ],
  "nested": {
    "flag": true,
    "random_number": 1
  }
}

The structure includes a title field, an array of companies, and a nested object with a flag and a random number.

VM

Matching infrastructure at centron

Node applications need somewhere to run: ccloud³ VMs with full root access from €3.12 per month – or as a managed server if you would rather not run it yourself. Rent a cloud server →

Android JSONObject

To parse JSON data in an Android application, we employ the JSONObject class from the Android SDK. In the presented example, we have defined a static JSON data string and utilize it to create a JSONObject. Subsequently, the data contained within is parsed and displayed in a ListView.

Example of Parsing JSON in Android

Code
// MainActivity.java
try {
    JSONObject root = new JSONObject(json_string);
    JSONArray array = root.getJSONArray("array");

    // Set the title of the activity
    this.setTitle(root.getString("title"));

    // Iterate through the array and add companies to the list
    for(int i=0; i<array.length(); i++) {
        JSONObject object = array.getJSONObject(i);
        items.add(object.getString("company"));
    }
} catch (JSONException e) {
    e.printStackTrace();
}

We iterate through the JSONArray object and add the contained strings to an ArrayList, which is displayed in the ListView. The activity title is also set according to the JSON data field.

Output of the Example

The application output displays the modified title in the toolbar at the top and the list of companies.

Conclusion

JSON stands as an integral part of modern Android development, particularly concerning communication with servers. The use of JSONObject facilitates parsing JSON data and its utilization in Android applications significantly. In this post, we've provided a basic overview of parsing JSON in Android applications and demonstrated how simple it is to extract and display data from JSON sources.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie JavaScript
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren