📄 jsonarray.java
字号:
* @return The truth. */ public boolean optBoolean(int index, boolean defaultValue) { Object o = opt(index); if (o != null) { if (o == Boolean.FALSE || (o instanceof String && ((String)o).equalsIgnoreCase("false"))) { return false; } else if (o == Boolean.TRUE || (o instanceof String && ((String)o).equalsIgnoreCase("true"))) { return true; } } return defaultValue; } /** * Get the optional double value associated with an index. * NaN is returned if the index is not found, * 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 optDouble(index, Double.NaN); } /** * Get the optional double value associated with an index. * The defaultValue is returned if the index is not found, * 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 o = opt(index); if (o != null) { if (o instanceof Number) { return ((Number) o).doubleValue(); } try { return new Double((String)o).doubleValue(); } catch (Exception e) { } } return defaultValue; } /** * Get the optional int value associated with an index. * Zero is returned if the index is not found, * 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 int optInt(int index) { return optInt(index, 0); } /** * Get the optional int value associated with an index. * The defaultValue is returned if the index is not found, * 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. * @param defaultValue The default value. * @return The value. */ public int optInt(int index, int defaultValue) { Object o = opt(index); if (o != null) { if (o instanceof Number) { return ((Number)o).intValue(); } try { return Integer.parseInt((String)o); } catch (Exception e) { } } return defaultValue; } /** * Get the optional JSONArray associated with an index. * @param index subscript * @return A JSONArray value, or null if the index has no value, * or if the value is not a JSONArray. */ public JSONArray optJSONArray(int index) { Object o = opt(index); if (o instanceof JSONArray) { return (JSONArray)o; } return null; } /** * Get the optional JSONObject associated with an index. * Null is returned if the key is not found, or null if the index has * no value, or if the value is not a JSONObject. * * @param index The index must be between 0 and length() - 1. * @return A JSONObject value. */ public JSONObject optJSONObject(int index) { Object o = opt(index); if (o instanceof JSONObject) { return (JSONObject)o; } return null; } /** * Get the optional string value associated with an index. It returns an * empty string if there is no value at that index. If the value * is not a string and is not null, then it is coverted to a string. * * @param index The index must be between 0 and length() - 1. * @return A String value. */ public String optString(int index){ return optString(index, ""); } /** * Get the optional string associated with an index. * The defaultValue is returned if the key is not found. * * @param index The index must be between 0 and length() - 1. * @param defaultValue The default value. * @return A String value. */ public String optString(int index, String defaultValue){ Object o = opt(index); if (o != null) { return o.toString(); } return defaultValue; } /** * Append a boolean value. * * @param value A boolean value. * @return this. */ public JSONArray put(boolean value) { put(new Boolean(value)); return this; } /** * Append a double value. * * @param value A double value. * @return this. */ public JSONArray put(double value) { put(new Double(value)); return this; } /** * Append an int value. * * @param value An int value. * @return this. */ public JSONArray put(int value) { put(new Integer(value)); return this; } /** * Append an object value. * @param value An object value. The value should be a * Boolean, Double, Integer, JSONArray, JSObject, or String, or the * JSONObject.NULL object. * @return this. */ public JSONArray put(Object value) { myArrayList.add(value); return this; } /** * Put or replace a boolean value in the JSONArray. * @exception NoSuchElementException The index must not be negative. * @param index subscript The subscript. If the index is greater than the length of * the JSONArray, then null elements will be added as necessary to pad * it out. * @param value A boolean value. * @return this. */ public JSONArray put(int index, boolean value) { put(index, new Boolean(value)); return this; } /** * Put or replace a double value. * @exception NoSuchElementException The index must not be negative. * @param index subscript The subscript. If the index is greater than the length of * the JSONArray, then null elements will be added as necessary to pad * it out. * @param value A double value. * return this. */ public JSONArray put(int index, double value) { put(index, new Double(value)); return this; } /** * Put or replace an int value. * @exception NoSuchElementException The index must not be negative. * @param index subscript The subscript. If the index is greater than the length of * the JSONArray, then null elements will be added as necessary to pad * it out. * @param value An int value. * @return this. */ public JSONArray put(int index, int value) { put(index, new Integer(value)); return this; } /** * Put or replace an object value in the JSONArray. * @exception NoSuchElementException The index must not be negative. * @param index The subscript. If the index is greater than the length of * the JSONArray, then null elements will be added as necessary to pad * it out. * @param value An object value. * return this. */ public JSONArray put(int index, Object value) throws NoSuchElementException, NullPointerException { if (index < 0) { throw new NoSuchElementException("JSONArray[" + index + "] not found."); } else if (value == null) { throw new NullPointerException(); } else if (index < length()) { myArrayList.set(index, value); } else { while (index != length()) { put(null); } put(value); } return this; } /** * Produce a JSONObject by combining a JSONArray of names with the values * of this JSONArray. * @param names A JSONArray containing a list of key strings. These will be * paired with the values. * @return A JSONObject, or null if there are no names or if this JSONArray * has no values. */ public JSONObject toJSONObject(JSONArray names) { if (names == null || names.length() == 0 || length() == 0) { return null; } JSONObject jo = new JSONObject(); for (int i = 0; i < names.length(); i += 1) { jo.put(names.getString(i), this.opt(i)); } return jo; } /** * Make an JSON external form string of this JSONArray. For compactness, no * unnecessary whitespace is added. * Warning: This method assumes that the data structure is acyclical. * * @return a printable, displayable, transmittable * representation of the array. */ public String toString() { return '[' + join(",") + ']'; } /** * Make a prettyprinted JSON string of this JSONArray. * Warning: This method assumes that the data structure is non-cyclical. * @param indentFactor The number of spaces to add to each level of * indentation. * @return a printable, displayable, transmittable * representation of the object, beginning * with <code>[</code> <small>(left bracket)</small> and ending * with <code>]</code> <small>(right bracket)</small>. */ public String toString(int indentFactor) { return toString(indentFactor, 0); } /** * Make a prettyprinted string of this JSONArray. * Warning: This method assumes that the data structure is non-cyclical. * @param indentFactor The number of spaces to add to each level of * indentation. * @param indent The indention of the top level. * @return a printable, displayable, transmittable * representation of the array. */ String toString(int indentFactor, int indent) { int i; Object o; String pad = ""; StringBuffer sb = new StringBuffer(); int newindent = indent + indentFactor; int len = myArrayList.size(); if (len == 0) { return "[]"; } for (i = 0; i < newindent; i += 1) { pad += ' '; } sb.append("[\n"); for (i = 0; i < len; i += 1) { if (i > 0) { sb.append(",\n"); } sb.append(pad); o = myArrayList.get(i); if (o == null) { sb.append("null"); } else if (o instanceof String) { sb.append(JSONObject.quote((String) o)); } else if (o instanceof Number) { sb.append(JSONObject.numberToString((Number) o)); } else if (o instanceof JSONObject) { sb.append(((JSONObject)o).toString(indentFactor, newindent)); } else if (o instanceof JSONArray) { sb.append(((JSONArray)o).toString(indentFactor, newindent)); } else { sb.append(o.toString()); } } sb.append('\n'); for (i = 0; i < indent; i += 1) { sb.append(' '); } sb.append(']'); return sb.toString(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -