How to iterate Json Array in Android

In this android programming source code example, we are going to iterate Json Array in Android.

You can copy and adopt this source code example to your android project without reinventing the wheel.

Below is a step by step source code to iterate Json Array in Android.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".json.Json3"
    android:layout_margin="16dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Iterate JSON Array"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:textColor="@color/colorAccent"
        android:id="@+id/text"/>

</LinearLayout>

MainActivity.java

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;

import com.inducesmile.inducesmile_button.R;

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

public class Json3 extends AppCompatActivity {

    String jsonString = "{ \"Employee\" :[{\"id\":\"101\",\"name\":\"Peter Dada\",\"salary\":\"50000\"}, {\"id\":\"102\",\"name\":\"John Smith\",\"salary\":\"60000\"}," +
            "{\"id\":\"103\",\"name\":\"Zacheus Zedd\",\"salary\":\"80000\"}] }";

    String className = Json3.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_json3);

        TextView textView = findViewById(R.id.text);

        try {
            JSONObject object = new JSONObject(jsonString);
            JSONArray array = object.optJSONArray("Employee");
            for (int i = 0; i < array.length(); i++) {
                JSONObject jsonObject = array.getJSONObject(i);

                int id = Integer.parseInt(jsonObject.optString("id"));
                String firstname = jsonObject.optString("name");
                float salary = Float.parseFloat(jsonObject.optString("salary"));

                String data = "";
                data += "Node" + i++ +" : \n id= "+ id +" \n Name= "+ firstname +" \n Salary= "+ salary +" \n ";
                textView.setText(data);
            }
        } catch (JSONException e) {
            Log.w(className, "Could not parse JSON. Error: " + e.getMessage());
            e.printStackTrace();
        }
    }
}

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

 

Add a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.