⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jsonobject.java

📁 sourcode about ajaxdojojson
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
    public JSONObject put(String key, double value) throws JSONException {        put(key, new Double(value));        return this;    }    /**     * Put a key/int pair in the JSONObject.     *     * @param key   A key string.     * @param value An int which is the value.     * @return this.     * @throws JSONException If the key is null.     */    public JSONObject put(String key, int value) throws JSONException {        put(key, new Integer(value));        return this;    }    /**     * Put a key/long pair in the JSONObject.     *     * @param key   A key string.     * @param value A long which is the value.     * @return this.     * @throws JSONException If the key is null.     */    public JSONObject put(String key, long value) throws JSONException {        put(key, new Long(value));        return this;    }    /**     * Put a key/value pair in the JSONObject. If the value is null,     * then the key will be removed from the JSONObject if it is present.     * @param key   A key string.     * @param value An object which is the value. It should be of one of these     *  types: Boolean, Double, Integer, JSONArray, JSONObject, Long, String,      *  or the JSONObject.NULL object.     * @return this.     * @throws JSONException If the value is non-finite number      * 	or if the key is null.     */    public JSONObject put(String key, Object value) throws JSONException {        if (key == null) {            throw new JSONException("Null key.");        }        if (value != null) {			testValidity(value);            this.myHashMap.put(key, value);        } else {            remove(key);        }        return this;    }    /**     * Put a key/value pair in the JSONObject, but only if the     * key and the value are both non-null.     * @param key   A key string.     * @param value An object which is the value. It should be of one of these     *  types: Boolean, Double, Integer, JSONArray, JSONObject, Long, String,      *  or the JSONObject.NULL object.     * @return this.     * @throws JSONException If the value is a non-finite number.     */    public JSONObject putOpt(String key, Object value) throws JSONException {        if (key != null && value != null) {            put(key, value);        }        return this;    }    /**     * Produce a string in double quotes with backslash sequences in all the     * right places. A backslash will be inserted within </, allowing JSON     * text to be delivered in HTML. In JSON text, a string cannot contain a     * control character or an unescaped quote or backslash.     * @param string A String     * @return  A String correctly formatted for insertion in a JSON text.     */    public static String quote(String string) {        if (string == null || string.length() == 0) {            return "\"\"";        }        char         b;        char         c = 0;        int          i;        int          len = string.length();        StringBuffer sb = new StringBuffer(len + 4);        String       t;        sb.append('"');        for (i = 0; i < len; i += 1) {            b = c;            c = string.charAt(i);            switch (c) {            case '\\':            case '"':                sb.append('\\');                sb.append(c);                break;            case '/':                if (b == '<') {                    sb.append('\\');                }                sb.append(c);                break;            case '\b':                sb.append("\\b");                break;            case '\t':                sb.append("\\t");                break;            case '\n':                sb.append("\\n");                break;            case '\f':                sb.append("\\f");                break;            case '\r':                sb.append("\\r");                break;            default:                if (c < ' ') {                    t = "000" + Integer.toHexString(c);                    sb.append("\\u" + t.substring(t.length() - 4));                } else {                    sb.append(c);                }            }        }        sb.append('"');        return sb.toString();    }    /**     * Remove a name and its value, if present.     * @param key The name to be removed.     * @return The value that was associated with the name,     * or null if there was no value.     */    public Object remove(String key) {        return this.myHashMap.remove(key);    }	/**	 * Throw an exception if the object is an NaN or infinite number.	 * @param o The object to test.	 * @throws JSONException If o is a non-finite number. 	 */	static void testValidity(Object o) throws JSONException {		if (o != null) {			if (o instanceof Double) {				if (((Double)o).isInfinite() || ((Double)o).isNaN()) {		            throw new JSONException(	                	"JSON does not allow non-finite numbers");				}			} else if (o instanceof Float) {				if (((Float)o).isInfinite() || ((Float)o).isNaN()) {		            throw new JSONException(	                	"JSON does not allow non-finite numbers.");				}			}		}	}		    /**     * Produce a JSONArray containing the values of the members of this     * JSONObject.     * @param names A JSONArray containing a list of key strings. This     * determines the sequence of the values in the result.     * @return A JSONArray of values.     * @throws JSONException If any of the values are non-finite numbers.     */    public JSONArray toJSONArray(JSONArray names) throws JSONException {        if (names == null || names.length() == 0) {            return null;        }        JSONArray ja = new JSONArray();        for (int i = 0; i < names.length(); i += 1) {            ja.put(this.opt(names.getString(i)));        }        return ja;    }    /**     * Make an JSON text of this JSONObject. For compactness, no whitespace     * is added. If this would not result in a syntactically correct JSON text,      * then null will be returned instead.     * <p>     * Warning: This method assumes that the data structure is acyclical.     *     * @return a printable, displayable, portable, transmittable     *  representation of the object, beginning     *  with <code>{</code>&nbsp;<small>(left brace)</small> and ending     *  with <code>}</code>&nbsp;<small>(right brace)</small>.     */    public String toString() {		try {	        Iterator     keys = keys();	        StringBuffer sb = new StringBuffer("{");		        while (keys.hasNext()) {	            if (sb.length() > 1) {	                sb.append(',');	            }	            Object o = keys.next();	            sb.append(quote(o.toString()));	            sb.append(':');	            sb.append(valueToString(this.myHashMap.get(o)));	        }	        sb.append('}');	        return sb.toString();		} catch (Exception e) {			return null;		}    }    /**     * Make a prettyprinted JSON text of this JSONObject.     * <p>     * Warning: This method assumes that the data structure is acyclical.     * @param indentFactor The number of spaces to add to each level of     *  indentation.     * @return a printable, displayable, portable, transmittable     *  representation of the object, beginning     *  with <code>{</code>&nbsp;<small>(left brace)</small> and ending     *  with <code>}</code>&nbsp;<small>(right brace)</small>.     * @throws JSONException If the object contains an invalid number.     */    public String toString(int indentFactor) throws JSONException {        return toString(indentFactor, 0);    }    /**     * Make a prettyprinted JSON text of this JSONObject.     * <p>     * Warning: This method assumes that the data structure is acyclical.     * @param indentFactor The number of spaces to add to each level of     *  indentation.     * @param indent The indentation of the top level.     * @return a printable, displayable, transmittable     *  representation of the object, beginning     *  with <code>{</code>&nbsp;<small>(left brace)</small> and ending     *  with <code>}</code>&nbsp;<small>(right brace)</small>.     * @throws JSONException If the object contains an invalid number.     */    String toString(int indentFactor, int indent) throws JSONException {        int          i;        int          n = length();        if (n == 0) {            return "{}";        }        Iterator     keys = keys();        StringBuffer sb = new StringBuffer("{");        int          newindent = indent + indentFactor;        Object       o;        if (n == 1) {            o = keys.next();            sb.append(quote(o.toString()));            sb.append(": ");            sb.append(valueToString(this.myHashMap.get(o), indentFactor, 					indent));        } else {            while (keys.hasNext()) {                o = keys.next();                if (sb.length() > 1) {                    sb.append(",\n");                } else {                    sb.append('\n');                }                for (i = 0; i < newindent; i += 1) {                    sb.append(' ');                }                sb.append(quote(o.toString()));                sb.append(": ");                sb.append(valueToString(this.myHashMap.get(o), indentFactor,                        newindent));            }            if (sb.length() > 1) {                sb.append('\n');                for (i = 0; i < indent; i += 1) {                    sb.append(' ');                }            }        }        sb.append('}');        return sb.toString();    }    /**     * Make a JSON text of an object value.     * <p>     * Warning: This method assumes that the data structure is acyclical.     * @param value The value to be serialized.     * @return a printable, displayable, transmittable     *  representation of the object, beginning     *  with <code>{</code>&nbsp;<small>(left brace)</small> and ending     *  with <code>}</code>&nbsp;<small>(right brace)</small>.     * @throws JSONException If the value is or contains an invalid number.     */    static String valueToString(Object value) throws JSONException {        if (value == null || value.equals(null)) {            return "null";        }        if (value instanceof Number) {            return numberToString((Number) value);        }        if (value instanceof Boolean || value instanceof JSONObject ||                value instanceof JSONArray) {            return value.toString();        }        return quote(value.toString());    }    /**     * Make a prettyprinted JSON text of an object value.     * <p>     * Warning: This method assumes that the data structure is acyclical.     * @param value The value to be serialized.     * @param indentFactor The number of spaces to add to each level of     *  indentation.     * @param indent The indentation of the top level.     * @return a printable, displayable, transmittable     *  representation of the object, beginning     *  with <code>{</code>&nbsp;<small>(left brace)</small> and ending     *  with <code>}</code>&nbsp;<small>(right brace)</small>.     * @throws JSONException If the object contains an invalid number.     */     static String valueToString(Object value, int indentFactor, int indent) 	 		throws JSONException {        if (value == null || value.equals(null)) {            return "null";        }        if (value instanceof Number) {            return numberToString((Number) value);        }        if (value instanceof Boolean) {            return value.toString();        }        if (value instanceof JSONObject) {            return ((JSONObject)value).toString(indentFactor, indent);        }        if (value instanceof JSONArray) {            return ((JSONArray)value).toString(indentFactor, indent);        }        return quote(value.toString());    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -