কম্পিউটার টিউটোরিয়াল

অ্যান্ড্রয়েডে JSONObject ব্যবহার করে JSON পার্স করা

JSONObject দিয়ে JSON ডেটা পার্স করা

এই টিউটোরিয়ালে দেখানো হয়েছে কীভাবে অ্যান্ড্রয়েডে org.json.JSONObject ব্যবহার করে JSON স্ট্রিং পার্স করা যায়।

প্রথম ধাপ: প্রজেক্ট তৈরি করুন

Android Studio-তে নতুন প্রজেক্ট তৈরি করুন।

দ্বিতীয় ধাপ: লেআউট ফাইল

res/layout/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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:textStyle="bold"
        android:textSize="24sp"
        android:layout_centerInParent="true"/>
</RelativeLayout>

তৃতীয় ধাপ: JSON পার্সিং কোড

src/MainActivity.java - হার্ডকোডেড JSON স্ট্রিং পার্স করা হয়েছে:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {
    public static final String JSON_STRING = "{\"Employee\":{\"Name\":\"Niyaz\",\"Salary\":56000}}";
    TextView textView;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.textView);
        
        try {
            // JSON অবজেক্ট থেকে Employee অবজেক্ট বের করা
            JSONObject emp = (new JSONObject(JSON_STRING)).getJSONObject("Employee");
            String empName = emp.getString("Name");
            int empSalary = emp.getInt("Salary");
            
            String string = "Employee Name: " + empName + "\n" + "Employee Salary: " + empSalary;
            textView.setText(string);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

চতুর্থ ধাপ: ম্যানিফেস্ট ফাইল

<?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>

অ্যাপ্লিকেশন চালান

Run করলে টেক্সটভিউতে JSON ডেটা থেকে বের করা নাম এবং বেতন দেখাবে।

অ্যান্ড্রয়েডে JSONObject ব্যবহার করে JSON পার্স করা

অতিরিক্ত টিপস

  • বাস্তব অ্যাপে JSON সাধারণত নেটওয়ার্ক থেকে বা ফাইল থেকে পড়ে আসা হয়
  • জটিল JSON স্ট্রাকচারের জন্য JSONArray এবং নেস্টেড JSONObject ব্যবহার করতে হয়
  • ত্রুটি হ্যান্ডলিং জন্য try-catch ব্লক অবশ্য ব্যবহার করুন
  1. Android TextView-এ startsWith() ব্যবহার কীভাবে করবেন?

  2. অ্যান্ড্রয়েডে ButterKnife লাইব্রেরি ব্যবহারের পদ্ধতি

  3. অ্যান্ড্রয়েডে JSON পার্স করার সহজ পদ্ধতি

  4. Android-এ Volley লাইব্রেরি ব্যবহার করে JSON পার্স করার পদ্ধতি