Java tutorial
//package com.java2s; //License from project: Apache License public class Main { /** * Safely converts an object into an double * * @param obj * The object to convert. * @return a Double representing the double value of the Object (null if the * object cannot be converted to Double) */ public static Double safeJsonToDouble(Object obj) { Double doubleValue; try { doubleValue = Double.parseDouble(safeJsonToString(obj)); } catch (NumberFormatException e) { doubleValue = null; } return doubleValue; } /** * 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()); } }