- if (nextChar != ']') {
- x.back();
- for (;;) {
- if (x.nextClean() == ',') {
- x.back();
- this.myArrayList.add(JSONObject.NULL);
- } else {
- x.back();
- this.myArrayList.add(x.nextValue());
- }
- switch (x.nextClean()) {
- case 0:
- // array is unclosed. No ']' found, instead EOF
- throw x.syntaxError("Expected a ',' or ']'");
- case ',':
- nextChar = x.nextClean();
- if (nextChar == 0) {
- // array is unclosed. No ']' found, instead EOF
- throw x.syntaxError("Expected a ',' or ']'");
- }
- if (nextChar == ']') {
- return;
- }
- x.back();
- break;
- case ']':
- return;
- default:
- throw x.syntaxError("Expected a ',' or ']'");
- }
- }
- }
- }
-
- /**
- * Construct a JSONArray from a source JSON text.
- *
- * @param source
- * A string that begins with <code>[</code> <small>(left
- * bracket)</small> and ends with <code>]</code>
- * <small>(right bracket)</small>.
- * @throws JSONException
- * If there is a syntax error.
- */
- public JSONArray(String source) throws JSONException {
- this(new JSONTokener(source));
- }
-
- /**
- * Construct a JSONArray from a Collection.
- *
- * @param collection
- * A Collection.
- */
- public JSONArray(Collection<?> collection) {
- if (collection == null) {
- this.myArrayList = new ArrayList<Object>();
- } else {
- this.myArrayList = new ArrayList<Object>(collection.size());
- for (Object o: collection){
- this.myArrayList.add(JSONObject.wrap(o));
- }
- }
- }
-
- /**
- * Construct a JSONArray from an array
- *
- * @throws JSONException
- * If not an array or if an array value is non-finite number.
- */
- public JSONArray(Object array) throws JSONException {
- this();
- if (array.getClass().isArray()) {
- int length = Array.getLength(array);
- this.myArrayList.ensureCapacity(length);
- for (int i = 0; i < length; i += 1) {
- this.put(JSONObject.wrap(Array.get(array, i)));
- }
- } else {
- throw new JSONException(
- "JSONArray initial value should be a string or collection or array.");
- }
- }
-
- @Override
- public Iterator<Object> iterator() {
- return this.myArrayList.iterator();
- }
-
- /**
- * Get the object value associated with an index.
- *
- * @param index
- * The index must be between 0 and length() - 1.
- * @return An object value.
- * @throws JSONException
- * If there is no value for the index.
- */
- public Object get(int index) throws JSONException {
- Object object = this.opt(index);
- if (object == null) {
- throw new JSONException("JSONArray[" + index + "] not found.");
- }
- return object;
- }
-
- /**
- * Get the boolean value associated with an index. The string values "true"
- * and "false" are converted to boolean.
- *
- * @param index
- * The index must be between 0 and length() - 1.
- * @return The truth.
- * @throws JSONException
- * If there is no value for the index or if the value is not
- * convertible to boolean.
- */
- public boolean getBoolean(int index) throws JSONException {
- Object object = this.get(index);
- if (object.equals(Boolean.FALSE)
- || (object instanceof String && ((String) object)
- .equalsIgnoreCase("false"))) {
- return false;
- } else if (object.equals(Boolean.TRUE)
- || (object instanceof String && ((String) object)
- .equalsIgnoreCase("true"))) {
- return true;
- }
- throw new JSONException("JSONArray[" + index + "] is not a boolean.");
- }
-
- /**
- * Get the double value associated with an index.
- *
- * @param index
- * The index must be between 0 and length() - 1.
- * @return The value.
- * @throws JSONException
- * If the key is not found or if the value cannot be converted
- * to a number.
- */
- public double getDouble(int index) throws JSONException {
- Object object = this.get(index);
- try {
- return object instanceof Number ? ((Number) object).doubleValue()
- : Double.parseDouble((String) object);
- } catch (Exception e) {
- throw new JSONException("JSONArray[" + index + "] is not a number.", e);
- }
- }
-
- /**
- * Get the float 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 float getFloat(int index) throws JSONException {
- Object object = this.get(index);
- try {
- return object instanceof Number ? ((Number) object).floatValue()
- : Float.parseFloat(object.toString());
- } catch (Exception e) {
- throw new JSONException("JSONArray[" + index
- + "] 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 the enum value associated with an index.
- *
- * @param clazz
- * The type of enum to retrieve.
- * @param index
- * The index must be between 0 and length() - 1.
- * @return The enum value at the index location
- * @throws JSONException
- * if the key is not found or if the value cannot be converted
- * to an enum.
- */
- public <E extends Enum<E>> E getEnum(Class<E> clazz, int index) throws JSONException {
- E val = optEnum(clazz, index);
- if(val==null) {
- // JSONException should really take a throwable argument.
- // If it did, I would re-implement this with the Enum.valueOf
- // method and place any thrown exception in the JSONException
- throw new JSONException("JSONArray[" + index + "] is not an enum of type "
- + JSONObject.quote(clazz.getSimpleName()) + ".");
- }
- return val;
- }
-
- /**
- * Get the BigDecimal value associated with an index.
- *
- * @param index
- * The index must be between 0 and length() - 1.
- * @return The value.
- * @throws JSONException
- * If the key is not found or if the value cannot be converted
- * to a BigDecimal.
- */
- public BigDecimal getBigDecimal (int index) throws JSONException {
- Object object = this.get(index);
- try {
- return new BigDecimal(object.toString());
- } catch (Exception e) {
- throw new JSONException("JSONArray[" + index +
- "] could not convert to BigDecimal.", e);
- }
- }
-
- /**
- * Get the BigInteger value associated with an index.
- *
- * @param index
- * The index must be between 0 and length() - 1.
- * @return The value.
- * @throws JSONException
- * If the key is not found or if the value cannot be converted
- * to a BigInteger.
- */
- public BigInteger getBigInteger (int index) throws JSONException {
- Object object = this.get(index);
- try {
- return new BigInteger(object.toString());
- } catch (Exception e) {
- throw new JSONException("JSONArray[" + index +
- "] could not convert to BigInteger.", e);
- }
- }
-
- /**
- * Get the int value associated with an index.
- *
- * @param index
- * The index must be between 0 and length() - 1.
- * @return The value.
- * @throws JSONException
- * If the key is not found or if the value is not a number.
- */
- public int getInt(int index) throws JSONException {
- Object object = this.get(index);
- try {
- return object instanceof Number ? ((Number) object).intValue()
- : Integer.parseInt((String) object);
- } catch (Exception e) {
- throw new JSONException("JSONArray[" + index + "] is not a number.", e);
- }
- }
-
- /**
- * Get the JSONArray associated with an index.
- *
- * @param index
- * The index must be between 0 and length() - 1.
- * @return A JSONArray value.
- * @throws JSONException
- * If there is no value for the index. or if the value is not a
- * JSONArray
- */
- public JSONArray getJSONArray(int index) throws JSONException {
- Object object = this.get(index);
- if (object instanceof JSONArray) {
- return (JSONArray) object;
- }
- throw new JSONException("JSONArray[" + index + "] is not a JSONArray.");
- }
-
- /**
- * Get the JSONObject associated with an index.
- *
- * @param index
- * subscript
- * @return A JSONObject value.
- * @throws JSONException
- * If there is no value for the index or if the value is not a
- * JSONObject
- */
- public JSONObject getJSONObject(int index) throws JSONException {
- Object object = this.get(index);
- if (object instanceof JSONObject) {
- return (JSONObject) object;
- }
- throw new JSONException("JSONArray[" + index + "] is not a JSONObject.");
- }
-
- /**
- * Get the long value associated with an index.
- *
- * @param index
- * The index must be between 0 and length() - 1.
- * @return The value.
- * @throws JSONException
- * If the key is not found or if the value cannot be converted
- * to a number.
- */
- public long getLong(int index) throws JSONException {
- Object object = this.get(index);
- try {
- return object instanceof Number ? ((Number) object).longValue()
- : Long.parseLong((String) object);
- } catch (Exception e) {
- throw new JSONException("JSONArray[" + index + "] is not a number.", e);
- }
- }
-
- /**
- * Get the string associated with an index.
- *
- * @param index
- * The index must be between 0 and length() - 1.
- * @return A string value.
- * @throws JSONException
- * If there is no string value for the index.
- */
- public String getString(int index) throws JSONException {
- Object object = this.get(index);
- if (object instanceof String) {
- return (String) object;
- }
- throw new JSONException("JSONArray[" + index + "] not a string.");
- }
-
- /**
- * Determine if the value is <code>null</code>.
- *
- * @param index
- * The index must be between 0 and length() - 1.
- * @return true if the value at the index is <code>null</code>, or if there is no value.
- */
- public boolean isNull(int index) {
- return JSONObject.NULL.equals(this.opt(index));
- }
-
- /**
- * Make a string from the contents of this JSONArray. The
- * <code>separator</code> string is inserted between each element. Warning:
- * This method assumes that the data structure is acyclical.
- *
- * @param separator
- * A string that will be inserted between the elements.
- * @return a string.
- * @throws JSONException
- * If the array contains an invalid number.
- */
- public String join(String separator) throws JSONException {
- int len = this.length();
- StringBuilder sb = new StringBuilder();
-
- for (int i = 0; i < len; i += 1) {
- if (i > 0) {
- sb.append(separator);
- }
- sb.append(JSONObject.valueToString(this.myArrayList.get(i)));
- }
- return sb.toString();
- }
-
- /**
- * Get the number of elements in the JSONArray, included nulls.
- *
- * @return The length (or size).
- */
- public int length() {
- return this.myArrayList.size();
- }
-
- /**
- * Get the optional object value associated with an index.
- *
- * @param index
- * The index must be between 0 and length() - 1. If not, null is returned.
- * @return An object value, or null if there is no object at that index.
- */
- public Object opt(int index) {
- return (index < 0 || index >= this.length()) ? null : this.myArrayList
- .get(index);
- }
-
- /**
- * Get the optional boolean value associated with an index. It returns false
- * if there is no value at that index, or if the value is not Boolean.TRUE
- * or the String "true".
- *
- * @param index
- * The index must be between 0 and length() - 1.
- * @return The truth.
- */
- public boolean optBoolean(int index) {
- return this.optBoolean(index, false);
- }
-
- /**
- * Get the optional boolean value associated with an index. It returns the
- * defaultValue if there is no value at that index or if it is not a Boolean
- * or the String "true" or "false" (case insensitive).
- *
- * @param index
- * The index must be between 0 and length() - 1.
- * @param defaultValue
- * A boolean default.
- * @return The truth.
- */
- public boolean optBoolean(int index, boolean defaultValue) {
- try {
- return this.getBoolean(index);
- } catch (Exception e) {
- return defaultValue;
- }
- }
-
- /**
- * Get the optional double value associated with an index. NaN is returned
- * if there is no value for the index, or if the value is not a number and
- * cannot be converted to a number.
- *
- * @param index
- * The index must be between 0 and length() - 1.
- * @return The value.
- */
- public double optDouble(int index) {
- return this.optDouble(index, Double.NaN);
- }
-
- /**
- * Get the optional double value associated with an index. The defaultValue
- * is returned if there is no value for the index, or if the value is not a
- * number and cannot be converted to a number.
- *
- * @param index
- * subscript
- * @param defaultValue
- * The default value.
- * @return The value.
- */
- public double optDouble(int index, double defaultValue) {
- Object val = this.opt(index);
- if (JSONObject.NULL.equals(val)) {
- return defaultValue;
- }
- if (val instanceof Number){
- return ((Number) val).doubleValue();
- }
- if (val instanceof String) {
- try {
- return Double.parseDouble((String) val);
- } catch (Exception e) {
- return defaultValue;
- }
- }
+ }
+ }
+ }
+
+ /**
+ * Construct a JSONArray from a source JSON text.
+ *
+ * @param source
+ * A string that begins with <code>[</code> <small>(left
+ * bracket)</small> and ends with <code>]</code> <small>(right
+ * bracket)</small>.
+ * @throws JSONException
+ * If there is a syntax error.
+ */
+ public JSONArray(String source) throws JSONException
+ {
+ this(new JSONTokener(source));
+ }
+
+ /**
+ * Construct a JSONArray from a Collection.
+ *
+ * @param collection
+ * A Collection.
+ */
+ public JSONArray(Collection<?> collection)
+ {
+ if (collection == null)
+ {
+ this.myArrayList = new ArrayList<Object>();
+ }
+ else
+ {
+ this.myArrayList = new ArrayList<Object>(collection.size());
+ for (Object o : collection)
+ {
+ this.myArrayList.add(JSONObject.wrap(o));
+ }
+ }
+ }
+
+ /**
+ * Construct a JSONArray from an array
+ *
+ * @throws JSONException
+ * If not an array or if an array value is non-finite number.
+ */
+ public JSONArray(Object array) throws JSONException
+ {
+ this();
+ if (array.getClass().isArray())
+ {
+ int length = Array.getLength(array);
+ this.myArrayList.ensureCapacity(length);
+ for (int i = 0; i < length; i += 1)
+ {
+ this.put(JSONObject.wrap(Array.get(array, i)));
+ }
+ }
+ else
+ {
+ throw new JSONException(
+ "JSONArray initial value should be a string or collection or array.");
+ }
+ }
+
+ @Override
+ public Iterator<Object> iterator()
+ {
+ return this.myArrayList.iterator();
+ }
+
+ /**
+ * Get the object value associated with an index.
+ *
+ * @param index
+ * The index must be between 0 and length() - 1.
+ * @return An object value.
+ * @throws JSONException
+ * If there is no value for the index.
+ */
+ public Object get(int index) throws JSONException
+ {
+ Object object = this.opt(index);
+ if (object == null)
+ {
+ throw new JSONException("JSONArray[" + index + "] not found.");
+ }
+ return object;
+ }
+
+ /**
+ * Get the boolean value associated with an index. The string values "true"
+ * and "false" are converted to boolean.
+ *
+ * @param index
+ * The index must be between 0 and length() - 1.
+ * @return The truth.
+ * @throws JSONException
+ * If there is no value for the index or if the value is not
+ * convertible to boolean.
+ */
+ public boolean getBoolean(int index) throws JSONException
+ {
+ Object object = this.get(index);
+ if (object.equals(Boolean.FALSE) || (object instanceof String
+ && ((String) object).equalsIgnoreCase("false")))
+ {
+ return false;
+ }
+ else if (object.equals(Boolean.TRUE) || (object instanceof String
+ && ((String) object).equalsIgnoreCase("true")))
+ {
+ return true;
+ }
+ throw new JSONException("JSONArray[" + index + "] is not a boolean.");
+ }
+
+ /**
+ * Get the double value associated with an index.
+ *
+ * @param index
+ * The index must be between 0 and length() - 1.
+ * @return The value.
+ * @throws JSONException
+ * If the key is not found or if the value cannot be converted to a
+ * number.
+ */
+ public double getDouble(int index) throws JSONException
+ {
+ Object object = this.get(index);
+ try
+ {
+ return object instanceof Number ? ((Number) object).doubleValue()
+ : Double.parseDouble((String) object);
+ } catch (Exception e)
+ {
+ throw new JSONException("JSONArray[" + index + "] is not a number.",
+ e);
+ }
+ }
+
+ /**
+ * Get the float 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 float getFloat(int index) throws JSONException
+ {
+ Object object = this.get(index);
+ try
+ {
+ return object instanceof Number ? ((Number) object).floatValue()
+ : Float.parseFloat(object.toString());
+ } catch (Exception e)
+ {
+ throw new JSONException("JSONArray[" + index + "] 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 the enum value associated with an index.
+ *
+ * @param clazz
+ * The type of enum to retrieve.
+ * @param index
+ * The index must be between 0 and length() - 1.
+ * @return The enum value at the index location
+ * @throws JSONException
+ * if the key is not found or if the value cannot be converted to an
+ * enum.
+ */
+ public <E extends Enum<E>> E getEnum(Class<E> clazz, int index)
+ throws JSONException
+ {
+ E val = optEnum(clazz, index);
+ if (val == null)
+ {
+ // JSONException should really take a throwable argument.
+ // If it did, I would re-implement this with the Enum.valueOf
+ // method and place any thrown exception in the JSONException
+ throw new JSONException(
+ "JSONArray[" + index + "] is not an enum of type "
+ + JSONObject.quote(clazz.getSimpleName()) + ".");
+ }
+ return val;
+ }
+
+ /**
+ * Get the BigDecimal value associated with an index.
+ *
+ * @param index
+ * The index must be between 0 and length() - 1.
+ * @return The value.
+ * @throws JSONException
+ * If the key is not found or if the value cannot be converted to a
+ * BigDecimal.
+ */
+ public BigDecimal getBigDecimal(int index) throws JSONException
+ {
+ Object object = this.get(index);
+ try
+ {
+ return new BigDecimal(object.toString());
+ } catch (Exception e)
+ {
+ throw new JSONException(
+ "JSONArray[" + index + "] could not convert to BigDecimal.",
+ e);
+ }
+ }
+
+ /**
+ * Get the BigInteger value associated with an index.
+ *
+ * @param index
+ * The index must be between 0 and length() - 1.
+ * @return The value.
+ * @throws JSONException
+ * If the key is not found or if the value cannot be converted to a
+ * BigInteger.
+ */
+ public BigInteger getBigInteger(int index) throws JSONException
+ {
+ Object object = this.get(index);
+ try
+ {
+ return new BigInteger(object.toString());
+ } catch (Exception e)
+ {
+ throw new JSONException(
+ "JSONArray[" + index + "] could not convert to BigInteger.",
+ e);
+ }
+ }
+
+ /**
+ * Get the int value associated with an index.
+ *
+ * @param index
+ * The index must be between 0 and length() - 1.
+ * @return The value.
+ * @throws JSONException
+ * If the key is not found or if the value is not a number.
+ */
+ public int getInt(int index) throws JSONException
+ {
+ Object object = this.get(index);
+ try
+ {
+ return object instanceof Number ? ((Number) object).intValue()
+ : Integer.parseInt((String) object);
+ } catch (Exception e)
+ {
+ throw new JSONException("JSONArray[" + index + "] is not a number.",
+ e);
+ }
+ }
+
+ /**
+ * Get the JSONArray associated with an index.
+ *
+ * @param index
+ * The index must be between 0 and length() - 1.
+ * @return A JSONArray value.
+ * @throws JSONException
+ * If there is no value for the index. or if the value is not a
+ * JSONArray
+ */
+ public JSONArray getJSONArray(int index) throws JSONException
+ {
+ Object object = this.get(index);
+ if (object instanceof JSONArray)
+ {
+ return (JSONArray) object;
+ }
+ throw new JSONException("JSONArray[" + index + "] is not a JSONArray.");
+ }
+
+ /**
+ * Get the JSONObject associated with an index.
+ *
+ * @param index
+ * subscript
+ * @return A JSONObject value.
+ * @throws JSONException
+ * If there is no value for the index or if the value is not a
+ * JSONObject
+ */
+ public JSONObject getJSONObject(int index) throws JSONException
+ {
+ Object object = this.get(index);
+ if (object instanceof JSONObject)
+ {
+ return (JSONObject) object;
+ }
+ throw new JSONException(
+ "JSONArray[" + index + "] is not a JSONObject.");
+ }
+
+ /**
+ * Get the long value associated with an index.
+ *
+ * @param index
+ * The index must be between 0 and length() - 1.
+ * @return The value.
+ * @throws JSONException
+ * If the key is not found or if the value cannot be converted to a
+ * number.
+ */
+ public long getLong(int index) throws JSONException
+ {
+ Object object = this.get(index);
+ try
+ {
+ return object instanceof Number ? ((Number) object).longValue()
+ : Long.parseLong((String) object);
+ } catch (Exception e)
+ {
+ throw new JSONException("JSONArray[" + index + "] is not a number.",
+ e);
+ }
+ }
+
+ /**
+ * Get the string associated with an index.
+ *
+ * @param index
+ * The index must be between 0 and length() - 1.
+ * @return A string value.
+ * @throws JSONException
+ * If there is no string value for the index.
+ */
+ public String getString(int index) throws JSONException
+ {
+ Object object = this.get(index);
+ if (object instanceof String)
+ {
+ return (String) object;
+ }
+ throw new JSONException("JSONArray[" + index + "] not a string.");
+ }
+
+ /**
+ * Determine if the value is <code>null</code>.
+ *
+ * @param index
+ * The index must be between 0 and length() - 1.
+ * @return true if the value at the index is <code>null</code>, or if there is
+ * no value.
+ */
+ public boolean isNull(int index)
+ {
+ return JSONObject.NULL.equals(this.opt(index));
+ }
+
+ /**
+ * Make a string from the contents of this JSONArray. The
+ * <code>separator</code> string is inserted between each element. Warning:
+ * This method assumes that the data structure is acyclical.
+ *
+ * @param separator
+ * A string that will be inserted between the elements.
+ * @return a string.
+ * @throws JSONException
+ * If the array contains an invalid number.
+ */
+ public String join(String separator) throws JSONException
+ {
+ int len = this.length();
+ StringBuilder sb = new StringBuilder();
+
+ for (int i = 0; i < len; i += 1)
+ {
+ if (i > 0)
+ {
+ sb.append(separator);
+ }
+ sb.append(JSONObject.valueToString(this.myArrayList.get(i)));
+ }
+ return sb.toString();
+ }
+
+ /**
+ * Get the number of elements in the JSONArray, included nulls.
+ *
+ * @return The length (or size).
+ */
+ public int length()
+ {
+ return this.myArrayList.size();
+ }
+
+ /**
+ * Get the optional object value associated with an index.
+ *
+ * @param index
+ * The index must be between 0 and length() - 1. If not, null is
+ * returned.
+ * @return An object value, or null if there is no object at that index.
+ */
+ public Object opt(int index)
+ {
+ return (index < 0 || index >= this.length()) ? null
+ : this.myArrayList.get(index);
+ }
+
+ /**
+ * Get the optional boolean value associated with an index. It returns false
+ * if there is no value at that index, or if the value is not Boolean.TRUE or
+ * the String "true".
+ *
+ * @param index
+ * The index must be between 0 and length() - 1.
+ * @return The truth.
+ */
+ public boolean optBoolean(int index)
+ {
+ return this.optBoolean(index, false);
+ }
+
+ /**
+ * Get the optional boolean value associated with an index. It returns the
+ * defaultValue if there is no value at that index or if it is not a Boolean
+ * or the String "true" or "false" (case insensitive).
+ *
+ * @param index
+ * The index must be between 0 and length() - 1.
+ * @param defaultValue
+ * A boolean default.
+ * @return The truth.
+ */
+ public boolean optBoolean(int index, boolean defaultValue)
+ {
+ try
+ {
+ return this.getBoolean(index);
+ } catch (Exception e)
+ {
+ return defaultValue;
+ }
+ }
+
+ /**
+ * Get the optional double value associated with an index. NaN is returned if
+ * there is no value for the index, or if the value is not a number and cannot
+ * be converted to a number.
+ *
+ * @param index
+ * The index must be between 0 and length() - 1.
+ * @return The value.
+ */
+ public double optDouble(int index)
+ {
+ return this.optDouble(index, Double.NaN);
+ }
+
+ /**
+ * Get the optional double value associated with an index. The defaultValue is
+ * returned if there is no value for the index, or if the value is not a
+ * number and cannot be converted to a number.
+ *
+ * @param index
+ * subscript
+ * @param defaultValue
+ * The default value.
+ * @return The value.
+ */
+ public double optDouble(int index, double defaultValue)
+ {
+ Object val = this.opt(index);
+ if (JSONObject.NULL.equals(val))
+ {
+ return defaultValue;
+ }
+ if (val instanceof Number)
+ {
+ return ((Number) val).doubleValue();
+ }
+ if (val instanceof String)
+ {
+ try
+ {
+ return Double.parseDouble((String) val);
+ } catch (Exception e)
+ {