String jsonString = "{\"name\":\"John\", \"age\":30, \"address\":{\"city\":\"New York\"}}"; JSONObject jsonObject = JSONObject.fromObject(jsonString); JSONObject addressObject = jsonObject.getJSONObject("address"); String city = addressObject.getString("city"); System.out.println(city); // Output: New York
String jsonString = "{\"students\":[{\"name\":\"John\", \"age\":30}, {\"name\":\"Jane\", \"age\":25}]}"; JSONObject jsonObject = JSONObject.fromObject(jsonString); JSONArray studentsArray = jsonObject.getJSONArray("students"); JSONObject studentObject = studentsArray.getJSONObject(0); String name = studentObject.getString("name"); System.out.println(name); // Output: JohnIn this example, we have a JSON string representing a list of students. We use the getJSONArray() method to retrieve the students array, and then use getJSONObject() to retrieve the first student object. We then use getString() to retrieve the student's name value. The package library used in these examples is net.sf.json.