Simple JSON Parsing Example in Android Studio
In this tutorial, we’ll learn how to parse a local JSON file in Android Studio and display the parsed data in a ListView. We'll keep it super simple for beginners.
📁 Step 1: JSON File – res/raw/data.json
Create a raw
folder inside res
and place this file in it.
data.json
[
{ "name": "Vipul", "age": 25, "city": "Bhavangar" },
{ "name": "Vivaan", "age": 30, "city": "Ahmedabad" },
{ "name": "Jatasya", "age": 22, "city": "Gandhinagar" }
]
🧱 Step 2: Layout File – activity_main.xml
This layout contains a simple ListView
to display our data.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
🧠 Step 3: Java Code – MainActivity.java
This is the main logic where we:
- Read the JSON from
raw
folder - Parse it using
JSONArray
andJSONObject
- Show the data in a ListView
MainActivity.java
package com.tecvipul.jsonparsingexample;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Scanner;
public class MainActivity extends AppCompatActivity {
ListView listView;
ArrayList<String> listData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.listView);
listData = new ArrayList<>();
String jsonString = readJsonFromRaw();
try {
JSONArray jsonArray = new JSONArray(jsonString);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject obj = jsonArray.getJSONObject(i);
String name = obj.getString("name");
int age = obj.getInt("age");
String city = obj.getString("city");
listData.add(name + " - " + age + " - " + city);
}
ArrayAdapter<String> adapter = new ArrayAdapter<>(
this, android.R.layout.simple_list_item_1, listData);
listView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
// Helper function to read JSON file from raw folder
private String readJsonFromRaw() {
InputStream inputStream = getResources().openRawResource(R.raw.data);
Scanner scanner = new Scanner(inputStream).useDelimiter("\\\\A");
return scanner.hasNext() ? scanner.next() : "";
}
}
📌 Output
When you run the app, it will display a list like this:
- Vipul - 25 - Bhavangar
- Vivaan - 30 - Ahmedabad
- Jatasya - 22 - Gandhinagar
✅ Summary
This is one of the easiest ways to get started with JSON parsing in Android using local files. No networking, no complexity. You just:
- Put a JSON file in
res/raw
- Read the file as a string
- Parse it using
org.json
- Show results in a
ListView
0 Comments
If you have any doubts, Please let me know