JSON.simple হল একটি সোজা এবং সহজ লাইব্রেরি যা JSON ডেটা তৈরি, পার্স এবং প্রক্রিয়া করার জন্য ব্যবহৃত হয়। JSON ডেটা সাধারণত key-value pairs আকারে থাকে, এবং JSON.simple এর মাধ্যমে আপনি এই key-value পেয়ার থেকে ডেটা অ্যাক্সেস করতে পারেন।
এই নিবন্ধে, আমরা দেখব কিভাবে JSON.simple ব্যবহার করে key-value pairs থেকে ডেটা অ্যাক্সেস করা যায়।
1. JSON.simple এর মাধ্যমে Key-Value পেয়ার তৈরি করা
প্রথমে একটি JSONObject তৈরি করতে হবে, যা key-value পেয়ার ধারণ করে। এরপর সেই JSON অবজেক্ট থেকে ডেটা অ্যাক্সেস করতে হবে।
Step 1: JSONObject তৈরি করা
import org.json.simple.JSONObject;
public class JsonExample {
public static void main(String[] args) {
// Create a new JSONObject
JSONObject obj = new JSONObject();
// Add key-value pairs to the JSONObject
obj.put("name", "John Doe");
obj.put("age", 30);
obj.put("city", "New York");
// Print the JSONObject
System.out.println(obj);
}
}
Output:
{"name":"John Doe","age":30,"city":"New York"}
এখানে, আমরা একটি JSONObject তৈরি করেছি এবং name, age, এবং city এর জন্য key-value পেয়ার সংরক্ষণ করেছি।
2. Key-Value পেয়ার থেকে ডেটা অ্যাক্সেস করা
একবার JSONObject তৈরি হয়ে গেলে, আপনি get() মেথড ব্যবহার করে নির্দিষ্ট key এর মাধ্যমে ডেটা অ্যাক্সেস করতে পারেন। JSON.simple এ key-value pairs থেকে ডেটা অ্যাক্সেস করার জন্য আপনাকে সঠিক key ব্যবহার করতে হবে।
Step 2: Key-Value পেয়ার থেকে ডেটা অ্যাক্সেস করা
import org.json.simple.JSONObject;
public class JsonExample {
public static void main(String[] args) {
// Create a new JSONObject
JSONObject obj = new JSONObject();
// Add key-value pairs to the JSONObject
obj.put("name", "John Doe");
obj.put("age", 30);
obj.put("city", "New York");
// Access data from JSONObject using key
String name = (String) obj.get("name");
Long age = (Long) obj.get("age");
String city = (String) obj.get("city");
// Print the accessed data
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("City: " + city);
}
}
Output:
Name: John Doe
Age: 30
City: New York
এখানে, obj.get("name"), obj.get("age"), এবং obj.get("city") ব্যবহার করে আমরা JSON অবজেক্ট থেকে ডেটা অ্যাক্সেস করেছি।
Note: JSON.simple-এ ডেটা অ্যাক্সেস করার সময়, ডেটার টাইপ অনুযায়ী কাস্টিং করতে হয়। উদাহরণস্বরূপ, age একটি Long টাইপ, তাই সেটিকে Long-এ কাস্ট করা হয়েছে।
3. JSON.simple এর মাধ্যমে Nested JSON Objects থেকে ডেটা Access করা
কখনও কখনও JSON অবজেক্টগুলির মধ্যে nested objects থাকে। এই ক্ষেত্রে, আপনি nested JSON objects থেকে ডেটা অ্যাক্সেস করতে পারেন।
Example: Nested JSON Object
import org.json.simple.JSONObject;
public class NestedJsonExample {
public static void main(String[] args) {
// Create a new JSONObject
JSONObject obj = new JSONObject();
// Create a nested JSONObject
JSONObject address = new JSONObject();
address.put("street", "123 Main St");
address.put("city", "New York");
address.put("zip", "10001");
// Add nested object to the main JSONObject
obj.put("name", "John Doe");
obj.put("age", 30);
obj.put("address", address);
// Access data from the nested JSONObject
String name = (String) obj.get("name");
Long age = (Long) obj.get("age");
JSONObject nestedAddress = (JSONObject) obj.get("address");
// Accessing nested data
String street = (String) nestedAddress.get("street");
String city = (String) nestedAddress.get("city");
String zip = (String) nestedAddress.get("zip");
// Print the accessed data
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Address: " + street + ", " + city + ", " + zip);
}
}
Output:
Name: John Doe
Age: 30
Address: 123 Main St, New York, 10001
এখানে, address একটি nested JSONObject ছিল, যা obj.put("address", address) এর মাধ্যমে প্রধান অবজেক্টে যোগ করা হয়েছিল। পরে, nestedAddress.get("street") ইত্যাদি ব্যবহার করে nested JSON object থেকে ডেটা অ্যাক্সেস করা হয়েছে।
4. JSONArray ব্যবহার করে ডেটা অ্যাক্সেস করা
JSON.simple JSONArray ব্যবহার করে ডেটার তালিকা (array) ধারণ করতে পারে। যদি আপনি একটি JSON array ব্যবহার করতে চান, তাহলে JSONArray ব্যবহার করবেন এবং এর মধ্যে JSON objects বা values যোগ করতে পারবেন।
Example: JSONArray with Key-Value pairs
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
public class JsonArrayExample {
public static void main(String[] args) {
// Create a new JSONObject
JSONObject obj = new JSONObject();
// Create a JSONArray
JSONArray employeeList = new JSONArray();
// Add employee details to the JSONArray
JSONObject employee1 = new JSONObject();
employee1.put("name", "John");
employee1.put("age", 30);
employee1.put("position", "Developer");
JSONObject employee2 = new JSONObject();
employee2.put("name", "Jane");
employee2.put("age", 28);
employee2.put("position", "Designer");
// Add employees to the array
employeeList.add(employee1);
employeeList.add(employee2);
// Add the array to the main JSONObject
obj.put("employees", employeeList);
// Access data from the JSONArray
JSONArray employees = (JSONArray) obj.get("employees");
JSONObject firstEmployee = (JSONObject) employees.get(0);
String name = (String) firstEmployee.get("name");
Long age = (Long) firstEmployee.get("age");
String position = (String) firstEmployee.get("position");
// Print the data from JSONArray
System.out.println("Employee Name: " + name);
System.out.println("Employee Age: " + age);
System.out.println("Employee Position: " + position);
}
}
Output:
Employee Name: John
Employee Age: 30
Employee Position: Developer
এখানে, একটি JSONArray তৈরি করা হয়েছে, যেখানে দুইটি JSONObject রয়েছে। প্রতিটি employee এর ডেটা অ্যাক্সেস করার জন্য JSONArray এর মধ্যে থেকে আমরা get(0) বা get(1) ব্যবহার করেছি।
- JSON.simple ব্যবহার করে আপনি key-value pairs থেকে ডেটা অ্যাক্সেস করতে পারেন, যেখানে JSONObject থেকে get() মেথড ব্যবহার করা হয়।
- nested JSON objects থেকে ডেটা অ্যাক্সেস করার জন্য আপনি nested get() মেথড ব্যবহার করতে পারেন।
- JSONArray ব্যবহার করে আপনি JSON arrays এর মধ্যে JSON objects বা values অ্যাক্সেস করতে পারেন।
- JSON.simple লাইব্রেরি ব্যবহার করে আপনি সহজেই JSON parsing, generation, এবং serialization/deserialization কার্যক্রম সম্পাদন করতে পারেন।
এই টিউটোরিয়াল আপনাকে JSON.simple দিয়ে key-value pairs থেকে ডেটা অ্যাক্সেস এবং কাস্টমাইজ করতে সাহায্য করবে।
Read more