public class JO { public static void main(String args[]) { JSONObject jo = new JSONObject(); jo.put("isRight", true); jo.getBoolean("isRight"); System.out.print("right"); } }
當運行上面的代碼的時候,後臺會報異常: java
Exception in thread "main" java.lang.ClassCastException: JSONObject["isRight"] is not a Boolean. at org.json.JSONObject.getBoolean(JSONObject.java:244) at test.basic.kownledge.JO.main(JO.java:9)
The values can be any of these types: Boolean, JSONArray, JSONObject, Number, String, or the JSONObject.NULL object
public class JO { public static void main(String args[]) { JSONObject jo = new JSONObject(); jo.put("isRight", Boolean.FALSE); jo.getBoolean("isRight"); System.out.print("right"); } }這樣寫就對了。