/** * 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 o = get(index); return o instanceof Number ? ((Number)o).longValue() : (long)getDouble(index); }
/** * 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 value for the index. */ public String getString(int index) throws JSONException { return get(index).toString(); }
/** * 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 o = get(index); try { return o instanceof Number ? ((Number)o).doubleValue() : Double.valueOf((String)o).doubleValue(); } catch (Exception e) { throw new JSONException("JSONArray[" + index + "] is not a number."); } }
/** * 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 cannot * be converted to a number. * if the value cannot be converted to a number. */ public int getInt(int index) throws JSONException { Object o = get(index); return o instanceof Number ? ((Number)o).intValue() : (int)getDouble(index); }
/** * 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 convertable to boolean. */ public boolean getBoolean(int index) throws JSONException { Object o = get(index); if (o.equals(Boolean.FALSE) || (o instanceof String && ((String)o).equalsIgnoreCase("false"))) { return false; } else if (o.equals(Boolean.TRUE) || (o instanceof String && ((String)o).equalsIgnoreCase("true"))) { return true; } throw new JSONException("JSONArray[" + index + "] is not a Boolean."); }
/** * 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(JSONArray ja, String separator) throws JSONException { final int len = ja.length(); StringBuffer sb = new StringBuffer(); for (int i = 0; i < len; i += 1) { if (i > 0) { sb.append(separator); } sb.append(JSONObject.valueToString(ja.get(i))); } return sb.toString(); }
writer.write(','); final Object v = ja.get(i); if (v instanceof JSONObject) { ((JSONObject)v).write(writer);
/** * 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 o = get(index); if (o instanceof JSONObject) { return (JSONObject) o; } else if (o instanceof String) { JSONTokener tokener = new JSONTokener((String) o); try { return new JSONObject(tokener); } catch (JSONException ignore) { // will throw the appropriate exception below } } throw new JSONException("JSONArray[" + index + "] is not a JSONObject."); }
/** * 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 o = get(index); if (o instanceof JSONArray) { return (JSONArray) o; } else if (o instanceof String) { JSONTokener tokener = new JSONTokener((String) o); try { return new JSONArray(tokener); } catch (JSONException ignore) { // will throw the appropriate exception below } } throw new JSONException("JSONArray[" + index + "] is not a JSONArray."); }
if (values.get(0) instanceof Integer) { props.put(propertyName, toArray(properties, propertyName, int.class)); else if (values.get(0) instanceof Long) { props.put(propertyName, toArray(properties, propertyName, long.class)); else if (values.get(0) instanceof Double) { props.put(propertyName, toArray(properties, propertyName, double.class)); else if (values.get(0) instanceof Boolean) { props.put(propertyName, toArray(properties, propertyName, boolean.class));
final Object[] values = new Object[array.length()]; for (int i = 0; i < array.length(); i++) { values[i] = array.get(i);