import org.json.JSONObject; // Create a JSONObject JSONObject obj = new JSONObject(); // Add keys and values obj.put("name", "John"); obj.put("age", 30); obj.put("isMarried", true);
import org.json.JSONObject; // Create a JSONObject JSONObject obj = new JSONObject("{\"name\":\"John\",\"age\":30,\"isMarried\":true}"); // Retrieve values using keys String name = obj.getString("name"); int age = obj.getInt("age"); boolean isMarried = obj.getBoolean("isMarried");
import org.json.JSONObject; // Create a nested JSONObject JSONObject obj = new JSONObject(); JSONObject address = new JSONObject(); address.put("street", "123 Main St"); address.put("city", "New York"); obj.put("name", "John"); obj.put("address", address);In all of these examples, the package library being used is org.json, which provides classes for working with JSON data in Java.