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