Java tutorial
//package com.java2s; //License from project: Apache License public class Main { /** * Safely converts an object into an boolean * * @param obj * The object to convert. * @return a Boolean representing the boolean value of the Object (null if * the object cannot be converted to Boolean) */ public static Boolean safeJsonToBoolean(Object obj) { Boolean booleanValue; try { booleanValue = Boolean.parseBoolean(safeJsonToString(obj)); } catch (NumberFormatException e) { booleanValue = null; } return booleanValue; } /** * Safely converts an object into string (used because sometimes * JSONObject's get() method returns null). * * @param obj * The object to convert. * @return The string. */ public static String safeJsonToString(Object obj) { return (obj == null ? null : obj.toString()); } }