public static Object stringToValue(String string) {
        if ("true".equalsIgnoreCase(string)) {
            return Boolean.TRUE;
        }
        if ("false".equalsIgnoreCase(string)) {
            return Boolean.FALSE;
        }
        if ("null".equalsIgnoreCase(string)) {
            return JSONObject.NULL;
        }

// If it might be a number, try converting it, first as a Long, and then as a
// Double. If that doesn't work, return the string.

        try {
            char initial = string.charAt(0);
            if (initial == '-' || (initial >= '0' && initial <= '9')) {
                Long value = new Long(string);
                if (value.toString().equals(string)) {
                    return value;
                }
            }
        }  catch (Exception ignore) {
            try {
                Double value = new Double(string);
                if (value.toString().equals(string)) {
                    return value;
                }
            }  catch (Exception ignoreAlso) {
            }
        }
        return string;
    }
public static Object stringToValue ( String string ) { if ( "true" . equalsIgnoreCase ( string ) ) { return Boolean . TRUE ; } if ( "false" . equalsIgnoreCase ( string ) ) { return Boolean . FALSE ; } if ( "null" . equalsIgnoreCase ( string ) ) { return JSONObject . NULL ; } try { char initial = string . charAt ( 0 ) ; if ( initial == '-' || ( initial >= '0' && initial <= '9' ) ) { Long value = new Long ( string ) ; if ( value . toString ( ) . equals ( string ) ) { return value ; } } } catch ( Exception ignore ) { try { Double value = new Double ( string ) ; if ( value . toString ( ) . equals ( string ) ) { return value ; } } catch ( Exception ignoreAlso ) { } } return string ; }
Try to convert a string into a number, boolean, or null. If the string
can't be converted, return the string. This is much less ambitious than
JSONObject.stringToValue, especially because it does not attempt to
convert plus forms, octal forms, hex forms, or E forms lacking decimal
points.
@param string A String.
@return A simple JSON value.