JSONObject json = new JSONObject("{\"name\":\"John\", \"age\":30, \"city\":\"New York\"}"); JSONObject cityObject = json.optJSONObject("city"); if(cityObject != null) { System.out.println(cityObject.getString("name")); }In this example, we create a JSONObject named "json" and retrieve a JSONObject value with the key "city" using the optJSONObject() method. We then check if the value is not null and proceed to retrieve the value for key "name" from the obtained JSONObject. The org.json package library is used in this example.