/** * Get the Number value associated with a key. * * @param index * The index must be between 0 and length() - 1. * @return The numeric value. * @throws JSONException * if the key is not found or if the value is not a Number * object and cannot be converted to a number. */ public Number getNumber(int index) throws JSONException { Object object = this.get(index); try { if (object instanceof Number) { return (Number)object; } return JSONObject.stringToNumber(object.toString()); } catch (Exception e) { throw new JSONException("JSONArray[" + index + "] is not a number.", e); } }
/** * Get an optional {@link Number} value associated with a key, or the default if there * is no such key or if the value is not a number. If the value is a string, * an attempt will be made to evaluate it as a number ({@link BigDecimal}). This method * would be used in cases where type coercion of the number value is unwanted. * * @param index * The index must be between 0 and length() - 1. * @param defaultValue * The default. * @return An object which is the value. */ public Number optNumber(int index, Number defaultValue) { Object val = this.opt(index); if (JSONObject.NULL.equals(val)) { return defaultValue; } if (val instanceof Number){ return (Number) val; } if (val instanceof String) { try { return JSONObject.stringToNumber((String) val); } catch (Exception e) { return defaultValue; } } return defaultValue; }
return stringToNumber((String) val); } catch (Exception e) { return defaultValue;
/** * Get the Number value associated with a key. * * @param key * A key string. * @return The numeric value. * @throws JSONException * if the key is not found or if the value is not a Number * object and cannot be converted to a number. */ public Number getNumber(String key) throws JSONException { Object object = this.get(key); try { if (object instanceof Number) { return (Number)object; } return stringToNumber(object.toString()); } catch (Exception e) { throw new JSONException("JSONObject[" + quote(key) + "] is not a number.", e); } }
/** * Get the Number value associated with a key. * * @param index * The index must be between 0 and length() - 1. * @return The numeric value. * @throws JSONException * if the key is not found or if the value is not a Number * object and cannot be converted to a number. */ public Number getNumber(int index) throws JSONException { Object object = this.get(index); try { if (object instanceof Number) { return (Number)object; } return JSONObject.stringToNumber(object.toString()); } catch (Exception e) { throw new JSONException("JSONArray[" + index + "] is not a number.", e); } }
/** * Get an optional {@link Number} value associated with a key, or the default if there * is no such key or if the value is not a number. If the value is a string, * an attempt will be made to evaluate it as a number. This method * would be used in cases where type coercion of the number value is unwanted. * * @param key * A key string. * @param defaultValue * The default. * @return An object which is the value. */ public Number optNumber(String key, Number defaultValue) { Object val = this.opt(key); if (NULL.equals(val)) { return defaultValue; } if (val instanceof Number){ return (Number) val; } try { return stringToNumber(val.toString()); } catch (Exception e) { return defaultValue; } }
/** * Get an optional {@link Number} value associated with a key, or the default if there * is no such key or if the value is not a number. If the value is a string, * an attempt will be made to evaluate it as a number ({@link BigDecimal}). This method * would be used in cases where type coercion of the number value is unwanted. * * @param index * The index must be between 0 and length() - 1. * @param defaultValue * The default. * @return An object which is the value. */ public Number optNumber(int index, Number defaultValue) { Object val = this.opt(index); if (JSONObject.NULL.equals(val)) { return defaultValue; } if (val instanceof Number){ return (Number) val; } if (val instanceof String) { try { return JSONObject.stringToNumber((String) val); } catch (Exception e) { return defaultValue; } } return defaultValue; }
/** * Get the Number value associated with a key. * * @param key * A key string. * @return The numeric value. * @throws JSONException * if the key is not found or if the value is not a Number * object and cannot be converted to a number. */ public Number getNumber(String key) throws JSONException { Object object = this.get(key); try { if (object instanceof Number) { return (Number)object; } return stringToNumber(object.toString()); } catch (Exception e) { throw new JSONException("JSONObject[" + quote(key) + "] is not a number.", e); } }
/** * Get the Number value associated with a key. * * @param index * The index must be between 0 and length() - 1. * @return The numeric value. * @throws JSONException * if the key is not found or if the value is not a Number * object and cannot be converted to a number. */ public Number getNumber(int index) throws JSONException { Object object = this.get(index); try { if (object instanceof Number) { return (Number)object; } return JSONObject.stringToNumber(object.toString()); } catch (Exception e) { throw new JSONException("JSONArray[" + index + "] is not a number.", e); } }
/** * Get an optional {@link Number} value associated with a key, or the default if there * is no such key or if the value is not a number. If the value is a string, * an attempt will be made to evaluate it as a number. This method * would be used in cases where type coercion of the number value is unwanted. * * @param key * A key string. * @param defaultValue * The default. * @return An object which is the value. */ public Number optNumber(String key, Number defaultValue) { Object val = this.opt(key); if (NULL.equals(val)) { return defaultValue; } if (val instanceof Number){ return (Number) val; } try { return stringToNumber(val.toString()); } catch (Exception e) { return defaultValue; } }
/** * Get an optional {@link Number} value associated with a key, or the default if there * is no such key or if the value is not a number. If the value is a string, * an attempt will be made to evaluate it as a number ({@link BigDecimal}). This method * would be used in cases where type coercion of the number value is unwanted. * * @param index * The index must be between 0 and length() - 1. * @param defaultValue * The default. * @return An object which is the value. */ public Number optNumber(int index, Number defaultValue) { Object val = this.opt(index); if (JSONObject.NULL.equals(val)) { return defaultValue; } if (val instanceof Number){ return (Number) val; } if (val instanceof String) { try { return JSONObject.stringToNumber((String) val); } catch (Exception e) { return defaultValue; } } return defaultValue; }
/** * Get the Number value associated with a key. * * @param key * A key string. * @return The numeric value. * @throws JSONException * if the key is not found or if the value is not a Number * object and cannot be converted to a number. */ public Number getNumber(String key) throws JSONException { Object object = this.get(key); try { if (object instanceof Number) { return (Number)object; } return stringToNumber(object.toString()); } catch (Exception e) { throw new JSONException("JSONObject[" + quote(key) + "] is not a number.", e); } }