String jsonStr = "{\"isTrue\": true}"; JSONObject jsonObj = new JSONObject(jsonStr); boolean isTrue = jsonObj.getBoolean("isTrue"); System.out.println("isTrue: " + isTrue);
String jsonStr = "{\"isTrue\": \"true\"}"; JSONObject jsonObj = new JSONObject(jsonStr); boolean isTrue = jsonObj.getBoolean("isTrue"); System.out.println("isTrue: " + isTrue);In this example, the value associated with the "isTrue" key is not a boolean value, but a string representing a boolean. The getBoolean() method will attempt to parse this value as a boolean and will throw a JSONException if it cannot be parsed. The org.json package is a third-party library that is not included in the Java Standard Edition API. It can be downloaded from the Maven repository or added as a dependency in your Java project.