import org.json.JSONObject;
import org.json.JSONArray;
import org.json.JSONException;
public class JettisonExample {
public static void main(String[] args) {
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("name", "John");
jsonObject.put("age", 25);
jsonObject.put("married", false);
JSONArray hobbies = new JSONArray();
hobbies.put("reading");
hobbies.put("coding");
jsonObject.put("hobbies", hobbies);
} catch (JSONException e) {
e.printStackTrace();
}
System.out.println(jsonObject.toString());
try {
String jsonStr = "{\"name\":\"John\",\"age\":25,\"married\":false,\"hobbies\":[\"reading\",\"coding\"]}";
JSONObject parsedObject = new JSONObject(jsonStr);
String name = parsedObject.getString("name");
int age = parsedObject.getInt("age");
boolean married = parsedObject.getBoolean("married");
JSONArray parsedHobbies = parsedObject.getJSONArray("hobbies");
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Married: " + married);
System.out.println("Hobbies: " + parsedHobbies.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
}