JSONArray পার্স করে ডেটা বের করা
JSON (JavaScript Object Notation) হল লাইটওয়েট ডেটা ইন্টারচেঞ্জ ফরম্যাট। অ্যান্ড্রয়েডে org.json লাইব্রেরি ব্যবহার করে JSON পার্স করা যায়।
ধাপ ১: নতুন প্রজেক্ট তৈরি করুন
Android Studio-তে একটি নতুন প্রজেক্ট শুরু করুন।
ধাপ ২: লেআউট XML (activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:textSize="16sp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
ধাপ ৩: MainActivity.java বাস্তবায়ন
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends AppCompatActivity {
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
// JSON স্ট্রিং (সাধারণত API থেকে আসে)
String strJson = "{ \"Basic info\" :[" +
"{\"Name\":\"Mash\",\"Age\":\"27\",\"Gender\":\"Male\"}," +
"{\"Name\":\"Niyaz\",\"Age\":\"23\",\"Gender\":\"Male\"}]}";
StringBuilder data = new StringBuilder();
try {
JSONObject jsonRootObject = new JSONObject(strJson);
JSONArray jsonArray = jsonRootObject.optJSONArray("Basic info");
if (jsonArray != null) {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
// নিরাপদে ডেটা বের করা
String name = jsonObject.optString("Name", "N/A");
String ageStr = jsonObject.optString("Age", "0");
int age = Integer.parseInt(ageStr);
String gender = jsonObject.optString("Gender", "N/A");
data.append("Node ").append(i).append(":\n");
data.append("Name: ").append(name).append("\n");
data.append("Age: ").append(age).append("\n");
data.append("Gender: ").append(gender).append("\n\n");
}
textView.setText(data.toString());
}
} catch (JSONException e) {
e.printStackTrace();
textView.setText("JSON Parsing Error: " + e.getMessage());
}
}
}
JSON পার্সিং-এর মূল ক্লাসগুলো
- JSONObject: কি-ভ্যালু পেয়ারের সংগ্রহ ({"key": "value"})
- JSONArray: অর্ডার্ড ভ্যালু লিস্ট (["value1", "value2"])
- optString()/optInt()/optJSONObject(): নাল চেক ছাড়া নিরাপদে ডেটা বের করতে
- getString()/getInt(): বাধ্যতামূলক ফিল্ডের জন্য (নাল হলে Exception ফেলে)
বেস্ট প্র্যাকটিস: Gson লাইব্রেরি ব্যবহার করুন
জটিল JSON-এর জন্য Google-এর Gson লাইব্রেরি ব্যবহার করা সহজ এবং দ্রুত:
// build.gradle-এ যোগ করুন
implementation 'com.google.code.gson:gson:2.10.1'
// মডেল ক্লাস
class User {
String name;
int age;
String gender;
}
// পার্সিং
Gson gson = new Gson();
Type listType = new TypeToken>(){}.getType();
List users = gson.fromJson(jsonArray.toString(), listType);
ধাপ ৪: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.sample">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
অ্যাপ্লিকেশন চালান
অ্যাপ রান করলে JSON ডেটা পার্স होकर TextView-এ ফরম্যাটেড ভাবে দেখাবে। দুটি ইউজারের নাম, বয়স এবং জেন্ডার প্রদর্শিত হবে।