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

📄 jsonobject.java

📁 hibernate+spring+ext2.0 的物流网站
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
        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 < ' ' || (c >= '\u0080' && c < '\u00a0') ||                               (c >= '\u2000' && c < '\u2100')) {                    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.map.remove(key);    }       /**     * Get an enumeration of the keys of the JSONObject.     * The keys will be sorted alphabetically.     *     * @return An iterator of the keys.     */    public Iterator sortedKeys() {      return new TreeSet(this.map.keySet()).iterator();    }    /**     * 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 a 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.map.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 j;        int n = length();        if (n == 0) {            return "{}";        }        Iterator     keys = sortedKeys();        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.map.get(o), indentFactor,                    indent));        } else {            while (keys.hasNext()) {                o = keys.next();                if (sb.length() > 1) {                    sb.append(",\n");                } else {                    sb.append('\n');                }                for (j = 0; j < newindent; j += 1) {                    sb.append(' ');                }                sb.append(quote(o.toString()));                sb.append(": ");                sb.append(valueToString(this.map.get(o), indentFactor,                        newindent));            }            if (sb.length() > 1) {                sb.append('\n');                for (j = 0; j < indent; j += 1) {                    sb.append(' ');                }            }        }        sb.append('}');        return sb.toString();    }    /**     * Make a JSON text of an Object value. If the object has an     * value.toJSONString() method, then that method will be used to produce     * the JSON text. The method is required to produce a strictly     * conforming text. If the object does not contain a toJSONString     * method (which is the most common case), then a text will be     * produced by other means. If the value is an array or Collection,     * then a JSONArray will be made from it and its toJSONString method     * will be called. If the value is a MAP, then a JSONObject will be made     * from it and its toJSONString method will be called. Otherwise, the     * value's toString method will be called, and the result will be quoted.     *     * <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 JSONString) {            Object o;            try {                o = ((JSONString)value).toJSONString();            } catch (Exception e) {                throw new JSONException(e);            }            if (o instanceof String) {                return (String)o;            }            throw new JSONException("Bad value from toJSONString: " + o);        }        if (value instanceof Number) {            return numberToString((Number) value);        }        if (value instanceof Boolean || value instanceof JSONObject ||                value instanceof JSONArray) {            return value.toString();        }        if (value instanceof Map) {            return new JSONObject((Map)value).toString();        }        if (value instanceof Collection) {            return new JSONArray((Collection)value).toString();        }        if (value.getClass().isArray()) {            return new JSONArray(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";        }        try {            if (value instanceof JSONString) {                Object o = ((JSONString)value).toJSONString();                if (o instanceof String) {                    return (String)o;                }            }        } catch (Exception e) {            /* forget about it */        }        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);        }        if (value instanceof Map) {            return new JSONObject((Map)value).toString(indentFactor, indent);        }        if (value instanceof Collection) {            return new JSONArray((Collection)value).toString(indentFactor, indent);        }        if (value.getClass().isArray()) {            return new JSONArray(value).toString(indentFactor, indent);        }        return quote(value.toString());    }     /**      * Write the contents of the JSONObject as JSON text to a writer.      * For compactness, no whitespace is added.      * <p>      * Warning: This method assumes that the data structure is acyclical.      *      * @return The writer.      * @throws JSONException      */     public Writer write(Writer writer) throws JSONException {        try {            boolean  b = false;            Iterator keys = keys();            writer.write('{');            while (keys.hasNext()) {                if (b) {                    writer.write(',');                }                Object k = keys.next();                writer.write(quote(k.toString()));                writer.write(':');                Object v = this.map.get(k);                if (v instanceof JSONObject) {                    ((JSONObject)v).write(writer);                } else if (v instanceof JSONArray) {                    ((JSONArray)v).write(writer);                } else {                    writer.write(valueToString(v));                }                b = true;            }            writer.write('}');            return writer;        } catch (IOException e) {            throw new JSONException(e);        }     }}

⌨️ 快捷键说明

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