import org.json.JSONArray;
import org.json.JSONObject;
public class JSONParser {
public static void main(String[] args) {
String jsonString = "{\"name\": \"John\", \"age\": 30, \"city\": \"New York\"}";
JSONObject jsonObject = new JSONObject(jsonString);
String name = jsonObject.getString("name");
int age = jsonObject.getInt("age");
String city = jsonObject.getString("city");
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("City: " + city);
}
}
import org.json.JSONObject;
public class JSONBuilder {
public static void main(String[] args) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("name", "John");
jsonObject.put("age", 30);
jsonObject.put("city", "New York");
String jsonString = jsonObject.toString();
System.out.println(jsonString);
}
}
import org.json.JSONArray;
import org.json.JSONObject;
public class NestedJSONProcessor {
public static void main(String[] args) {
String jsonString = "{\"name\": \"John\", \"age\": 30, \"addresses\": [{\"city\": \"New York\"}, {\"city\": \"London\"}]}";
JSONObject jsonObject = new JSONObject(jsonString);
String name = jsonObject.getString("name");
int age = jsonObject.getInt("age");
JSONArray addressesArray = jsonObject.getJSONArray("addresses");
for (int i = 0; i < addressesArray.length(); i++) {
JSONObject address = addressesArray.getJSONObject(i);
String city = address.getString("city");
System.out.println("City: " + city);
}
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}