Android में JSON डेटा पार्स करना - JSONArray और JSONObject
यह उदाहरण दर्शाता है कि Android में org.json पैकेज का उपयोग करके JSON स्ट्रिंग से डेटा कैसे निकाला जाए।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project चुनें।
चरण 2: लेआउट फ़ाइल (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:layout_centerInParent="true"
android:textSize="18sp"
android:textStyle="bold"
android:padding="16dp"/>
</RelativeLayout>
चरण 3: MainActivity में JSON पार्सिंग (src/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 {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
// JSON स्ट्रिंग - सही एस्केपिंग के साथ
String jsonString = "{ \"Basic info\" : [" +
"{\"Name\":\"Mash\",\"Age\":\"27\",\"Gender\":\"Male\"}," +
"{\"Name\":\"Niyaz\",\"Age\":\"23\",\"Gender\":\"Male\"}]}";
StringBuilder result = new StringBuilder();
try {
// रूट JSONObject बनाएं
JSONObject jsonRoot = new JSONObject(jsonString);
// "Basic info" JSONArray प्राप्त करें
JSONArray jsonArray = jsonRoot.optJSONArray("Basic info");
if (jsonArray != null) {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject person = jsonArray.getJSONObject(i);
// सुरक्षित तरीके से मान निकालें
String name = person.optString("Name", "N/A");
String ageStr = person.optString("Age", "0");
int age = Integer.parseInt(ageStr);
String gender = person.optString("Gender", "N/A");
result.append("रिकॉर्ड ").append(i + 1).append(":\n")
.append(" नाम: ").append(name).append("\n")
.append(" उम्र: ").append(age).append("\n")
.append(" लिंग: ").append(gender).append("\n\n");
}
textView.setText(result.toString());
}
} catch (JSONException e) {
e.printStackTrace();
textView.setText("JSON पार्सिंग त्रुटि: " + e.getMessage());
}
}
}
JSON पार्सिंग के मुख्य क्लास
| क्लास | उपयोग |
|---|---|
JSONObject |
की-वैल्यू पेयर ऑब्जेक्ट ({ }) |
JSONArray |
वैल्यूज की सरणी ([ ]) |
optString() |
सुरक्षित स्ट्रिंग प्राप्ति (डिफ़ॉल्ट के साथ) |
optInt() |
सुरक्षित इंटीजर प्राप्ति |
optJSONArray() |
सुरक्षित ऐरे प्राप्ति |
optXxx() मेथड्स का उपयोग करें (जैसे optString(), optInt()) क्योंकि ये की न मिलने पर क्रैश नहीं करते और डिफ़ॉल्ट मान लौटाते हैं। getString() आदि तभी उपयोग करें जब की निश्चित रूप से मौजूद हो।
चरण 4: 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 में फॉर्मेट होकर देखें।
