程序包 com.sh.json

类 JSONObject

所有已实现的接口:
JSON, Serializable, Comparable, Map

public class JSONObject extends AbstractJSON implements JSON, Map, Comparable
A JSONObject is an unordered collection of name/value pairs.
另请参阅:
  • 构造器详细资料

    • JSONObject

      public JSONObject()
      Construct an empty JSONObject.
    • JSONObject

      public JSONObject(Map<String,Object> map)
    • JSONObject

      public JSONObject(boolean isNull)
      Creates a JSONObject that is null.
  • 方法详细资料

    • fromObject

      public static JSONObject fromObject(Object object)
      Creates a JSONObject.
      Inspects the object type to call the correct JSONObject factory method. Accepts JSON formatted strings, Maps, DynaBeans and JavaBeans.
      参数:
      object -
      抛出:
      JSONException - if the object can not be converted to a proper JSONObject.
    • parseObject

      public static JSONObject parseObject(Object object)
      兼容fastjson
      参数:
      object -
      返回:
    • toJSON

      public static JSONObject toJSON(Object object)
      兼容fastjson
      参数:
      object -
      返回:
    • fromObject

      public static JSONObject fromObject(Object object, JsonConfig jsonConfig)
      Creates a JSONObject.
      Inspects the object type to call the correct JSONObject factory method. Accepts JSON formatted strings, Maps, DynaBeans and JavaBeans.
      参数:
      object -
      抛出:
      JSONException - if the object can not be converted to a proper JSONObject.
    • toBean

      protected static Object toBean(JSONObject jsonObject)
      Creates a JSONDynaBean from a JSONObject.
    • toBean

      public static <T> T toBean(JSONObject jsonObject, Class<T> beanClass)
      Creates a bean from a JSONObject, with a specific target class.
    • toBean

      public static <T> T toBean(String jsonStr, Class<T> beanClass)
    • toBean

      protected static Object toBean(JSONObject jsonObject, Class beanClass, Map classMap)
      Creates a bean from a JSONObject, with a specific target class.
      If beanClass is null, this method will return a graph of DynaBeans. Any attribute that is a JSONObject and matches a key in the classMap will be converted to that target class.
      The classMap has the following conventions:
      • Every key must be an String.
      • Every value must be a Class.
      • A key may be a regular expression.
    • toBean

      protected static Object toBean(JSONObject jsonObject, JsonConfig jsonConfig)
      Creates a bean from a JSONObject, with the specific configuration.
    • toBean

      protected static Object toBean(JSONObject jsonObject, Object root, JsonConfig jsonConfig)
      Creates a bean from a JSONObject, with the specific configuration.
    • accumulate

      public JSONObject accumulate(String key, boolean value)
      Accumulate values under a key. It is similar to the element method except that if there is already an object stored under the key then a JSONArray is stored under the key to hold all of the accumulated values. If there is already a JSONArray, then the new value is appended to it. In contrast, the replace method replaces the previous value.
      参数:
      key - A key string.
      value - An object to be accumulated under the key.
      返回:
      this.
      抛出:
      JSONException - If the value is an invalid number or if the key is null.
    • accumulate

      public JSONObject accumulate(String key, double value)
      Accumulate values under a key. It is similar to the element method except that if there is already an object stored under the key then a JSONArray is stored under the key to hold all of the accumulated values. If there is already a JSONArray, then the new value is appended to it. In contrast, the replace method replaces the previous value.
      参数:
      key - A key string.
      value - An object to be accumulated under the key.
      返回:
      this.
      抛出:
      JSONException - If the value is an invalid number or if the key is null.
    • accumulate

      public JSONObject accumulate(String key, int value)
      Accumulate values under a key. It is similar to the element method except that if there is already an object stored under the key then a JSONArray is stored under the key to hold all of the accumulated values. If there is already a JSONArray, then the new value is appended to it. In contrast, the replace method replaces the previous value.
      参数:
      key - A key string.
      value - An object to be accumulated under the key.
      返回:
      this.
      抛出:
      JSONException - If the value is an invalid number or if the key is null.
    • accumulate

      public JSONObject accumulate(String key, long value)
      Accumulate values under a key. It is similar to the element method except that if there is already an object stored under the key then a JSONArray is stored under the key to hold all of the accumulated values. If there is already a JSONArray, then the new value is appended to it. In contrast, the replace method replaces the previous value.
      参数:
      key - A key string.
      value - An object to be accumulated under the key.
      返回:
      this.
      抛出:
      JSONException - If the value is an invalid number or if the key is null.
    • accumulate

      public JSONObject accumulate(String key, Object value)
      Accumulate values under a key. It is similar to the element method except that if there is already an object stored under the key then a JSONArray is stored under the key to hold all of the accumulated values. If there is already a JSONArray, then the new value is appended to it. In contrast, the replace method replaces the previous value.
      参数:
      key - A key string.
      value - An object to be accumulated under the key.
      返回:
      this.
      抛出:
      JSONException - If the value is an invalid number or if the key is null.
    • accumulate

      public JSONObject accumulate(String key, Object value, JsonConfig jsonConfig)
      Accumulate values under a key. It is similar to the element method except that if there is already an object stored under the key then a JSONArray is stored under the key to hold all of the accumulated values. If there is already a JSONArray, then the new value is appended to it. In contrast, the replace method replaces the previous value.
      参数:
      key - A key string.
      value - An object to be accumulated under the key.
      返回:
      this.
      抛出:
      JSONException - If the value is an invalid number or if the key is null.
    • accumulateAll

      public void accumulateAll(Map map)
    • accumulateAll

      public void accumulateAll(Map map, JsonConfig jsonConfig)
    • clear

      public void clear()
      指定者:
      clear 在接口中 Map
    • compareTo

      public int compareTo(Object obj)
      指定者:
      compareTo 在接口中 Comparable
    • containsKey

      public boolean containsKey(Object key)
      指定者:
      containsKey 在接口中 Map
    • containsValue

      public boolean containsValue(Object value)
      指定者:
      containsValue 在接口中 Map
    • containsValue

      public boolean containsValue(Object value, JsonConfig jsonConfig)
    • discard

      public JSONObject discard(String key)
      Remove a name and its value, if present.
      参数:
      key - A key string.
      返回:
      this.
    • element

      public JSONObject element(String key, boolean value)
      Put a key/boolean pair in the JSONObject.
      参数:
      key - A key string.
      value - A boolean which is the value.
      返回:
      this.
      抛出:
      JSONException - If the key is null.
    • element

      public JSONObject element(String key, Collection value)
      Put a key/value pair in the JSONObject, where the value will be a JSONArray which is produced from a Collection.
      参数:
      key - A key string.
      value - A Collection value.
      返回:
      this.
      抛出:
      JSONException
    • element

      public JSONObject element(String key, Collection value, JsonConfig jsonConfig)
      Put a key/value pair in the JSONObject, where the value will be a JSONArray which is produced from a Collection.
      参数:
      key - A key string.
      value - A Collection value.
      返回:
      this.
      抛出:
      JSONException
    • element

      public JSONObject element(String key, double value)
      Put a key/double pair in the JSONObject.
      参数:
      key - A key string.
      value - A double which is the value.
      返回:
      this.
      抛出:
      JSONException - If the key is null or if the number is invalid.
    • element

      public JSONObject element(String key, int value)
      Put a key/int pair in the JSONObject.
      参数:
      key - A key string.
      value - An int which is the value.
      返回:
      this.
      抛出:
      JSONException - If the key is null.
    • element

      public JSONObject element(String key, long value)
      Put a key/long pair in the JSONObject.
      参数:
      key - A key string.
      value - A long which is the value.
      返回:
      this.
      抛出:
      JSONException - If the key is null.
    • element

      public JSONObject element(String key, Map value)
      Put a key/value pair in the JSONObject, where the value will be a JSONObject which is produced from a Map.
      参数:
      key - A key string.
      value - A Map value.
      返回:
      this.
      抛出:
      JSONException
    • element

      public JSONObject element(String key, Map value, JsonConfig jsonConfig)
      Put a key/value pair in the JSONObject, where the value will be a JSONObject which is produced from a Map.
      参数:
      key - A key string.
      value - A Map value.
      返回:
      this.
      抛出:
      JSONException
    • element

      public JSONObject element(String key, Object value)
      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.
      If there is a previous value assigned to the key, it will call accumulate.
      参数:
      key - A key string.
      value - An object which is the value. It should be of one of these types: Boolean, Double, Integer, JSONArray, JSONObject, Long, String, or the JSONNull object.
      返回:
      this.
      抛出:
      JSONException - If the value is non-finite number or if the key is null.
    • element

      public JSONObject element(String key, Object value, JsonConfig jsonConfig)
      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.
      If there is a previous value assigned to the key, it will call accumulate.
      参数:
      key - A key string.
      value - An object which is the value. It should be of one of these types: Boolean, Double, Integer, JSONArray, JSONObject, Long, String, or the JSONNull object.
      返回:
      this.
      抛出:
      JSONException - If the value is non-finite number or if the key is null.
    • elementOpt

      public JSONObject elementOpt(String key, Object value)
      Put a key/value pair in the JSONObject, but only if the key and the value are both non-null.
      参数:
      key - A key string.
      value - An object which is the value. It should be of one of these types: Boolean, Double, Integer, JSONArray, JSONObject, Long, String, or the JSONNull object.
      返回:
      this.
      抛出:
      JSONException - If the value is a non-finite number.
    • elementOpt

      public JSONObject elementOpt(String key, Object value, JsonConfig jsonConfig)
      Put a key/value pair in the JSONObject, but only if the key and the value are both non-null.
      参数:
      key - A key string.
      value - An object which is the value. It should be of one of these types: Boolean, Double, Integer, JSONArray, JSONObject, Long, String, or the JSONNull object.
      返回:
      this.
      抛出:
      JSONException - If the value is a non-finite number.
    • entrySet

      public Set<Map.Entry<String,Object>> entrySet()
      指定者:
      entrySet 在接口中 Map
    • equals

      public boolean equals(Object obj)
      指定者:
      equals 在接口中 Map
      覆盖:
      equals 在类中 Object
    • get

      public Object get(Object key)
      指定者:
      get 在接口中 Map
    • get

      public Object get(String key)
      Get the value object associated with a key.
      参数:
      key - A key string.
      返回:
      The object associated with the key.
      抛出:
      JSONException - if this.isNull() returns true.
    • getBoolean

      public boolean getBoolean(String key)
      Get the boolean value associated with a key.
      参数:
      key - A key string.
      返回:
      The truth.
      抛出:
      JSONException - if the value is not a Boolean or the String "true" or "false".
    • getBoolean

      public boolean getBoolean(String key, boolean defaultValue)
    • getDouble

      public double getDouble(String key)
      Get the double value associated with a key.
      参数:
      key - A key string.
      返回:
      The numeric value.
      抛出:
      JSONException - if the key is not found or if the value is not a Number object and cannot be converted to a number.
    • getDouble

      public double getDouble(String key, double defaultValue)
    • getInt

      public int getInt(String key)
      Get the int value associated with a key. If the number value is too large for an int, it will be clipped.
      参数:
      key - A key string.
      返回:
      The integer value.
      抛出:
      JSONException - if the key is not found or if the value cannot be converted to an integer.
    • getIntValue

      public int getIntValue(String key)
    • getInt

      public int getInt(String key, int defaultValue)
    • getInteger

      public Integer getInteger(String key)
    • getJSONArray

      public JSONArray getJSONArray(String key)
      Get the JSONArray value associated with a key.
      参数:
      key - A key string.
      返回:
      A JSONArray which is the value.
      抛出:
      JSONException - if the key is not found or if the value is not a JSONArray.
    • getJSONObject

      public JSONObject getJSONObject(String key)
      Get the JSONObject value associated with a key.
      参数:
      key - A key string.
      返回:
      A JSONObject which is the value.
      抛出:
      JSONException - if the key is not found or if the value is not a JSONObject.
    • getLong

      public long getLong(String key)
      Get the long value associated with a key. If the number value is too long for a long, it will be clipped.
      参数:
      key - A key string.
      返回:
      The long value.
      抛出:
      JSONException - if the key is not found or if the value cannot be converted to a long.
    • getLong

      public long getLong(String key, long defaultValue)
    • getString

      public String getString(String key)
      Get the string associated with a key.
      参数:
      key - A key string.
      返回:
      A string which is the value.
      抛出:
      JSONException - if the key is not found.
    • getString

      public String getString(String key, String defaultValue)
    • getStr

      public String getStr(String key)
    • getStr

      public String getStr(String key, String defaultValue)
    • has

      public boolean has(String key)
      Determine if the JSONObject contains a specific key.
      参数:
      key - A key string.
      返回:
      true if the key exists in the JSONObject.
    • hashCode

      public int hashCode()
      指定者:
      hashCode 在接口中 Map
      覆盖:
      hashCode 在类中 Object
    • isArray

      public boolean isArray()
      从接口复制的说明: JSON
      Returns true if this object is a JSONArray, false otherwise.
      指定者:
      isArray 在接口中 JSON
    • isEmpty

      public boolean isEmpty()
      从接口复制的说明: JSON
      Returns true if this object has no elements or keys.
      指定者:
      isEmpty 在接口中 JSON
      指定者:
      isEmpty 在接口中 Map
    • isNullObject

      public boolean isNullObject()
      Returs if this object is a null JSONObject.
    • keys

      public Iterator keys()
      Get an enumeration of the keys of the JSONObject.
      返回:
      An iterator of the keys.
    • keySet

      public Set<String> keySet()
      指定者:
      keySet 在接口中 Map
    • names

      public JSONArray names()
      Produce a JSONArray containing the names of the elements of this JSONObject.
      返回:
      A JSONArray containing the key strings, or null if the JSONObject is empty.
    • names

      public JSONArray names(JsonConfig jsonConfig)
      Produce a JSONArray containing the names of the elements of this JSONObject.
      返回:
      A JSONArray containing the key strings, or null if the JSONObject is empty.
    • opt

      public Object opt(String key)
      Get an optional value associated with a key.
      参数:
      key - A key string.
      返回:
      An object which is the value, or null if there is no value.
    • optBoolean

      public boolean optBoolean(String key)
      Get an optional boolean associated with a key. It returns false if there is no such key, or if the value is not Boolean.TRUE or the String "true".
      参数:
      key - A key string.
      返回:
      The truth.
    • optBoolean

      public boolean optBoolean(String key, boolean defaultValue)
      Get an optional boolean associated with a key. It returns the defaultValue if there is no such key, or if it is not a Boolean or the String "true" or "false" (case insensitive).
      参数:
      key - A key string.
      defaultValue - The default.
      返回:
      The truth.
    • optDouble

      public double optDouble(String key)
    • optDouble

      public double optDouble(String key, double defaultValue)
      Get an optional double associated with a key, or the defaultValue if there is no such key or if its value is not a number. If the value is a string, an attempt will be made to evaluate it as a number.
      参数:
      key - A key string.
      defaultValue - The default.
      返回:
      An object which is the value.
    • optInt

      public int optInt(String key)
    • optInt

      public int optInt(String key, int defaultValue)
    • optJSONArray

      public JSONArray optJSONArray(String key)
      Get an optional JSONArray associated with a key. It returns null if there is no such key, or if its value is not a JSONArray.
      参数:
      key - A key string.
      返回:
      A JSONArray which is the value.
    • optJSONObject

      public JSONObject optJSONObject(String key)
      Get an optional JSONObject associated with a key. It returns null if there is no such key, or if its value is not a JSONObject.
      参数:
      key - A key string.
      返回:
      A JSONObject which is the value.
    • optLong

      public long optLong(String key)
      Get an optional long value associated with a key, or zero if there is no such key or if the value is not a number. If the value is a string, an attempt will be made to evaluate it as a number.
      参数:
      key - A key string.
      返回:
      An object which is the value.
    • optLong

      public long optLong(String key, long defaultValue)
      Get an optional long value associated with a key, or the default if there is no such key or if the value is not a number. If the value is a string, an attempt will be made to evaluate it as a number.
      参数:
      key - A key string.
      defaultValue - The default.
      返回:
      An object which is the value.
    • optString

      public String optString(String key)
      Get an optional string associated with a key. It returns an empty string if there is no such key. If the value is not a string and is not null, then it is coverted to a string.
      参数:
      key - A key string.
      返回:
      A string which is the value.
    • optString

      public String optString(String key, String defaultValue)
      Get an optional string associated with a key. It returns the defaultValue if there is no such key.
      参数:
      key - A key string.
      defaultValue - The default.
      返回:
      A string which is the value.
    • put

      public Object put(Object key, Object value)
      指定者:
      put 在接口中 Map
    • put

      public Object put(Object key, Object value, JsonConfig config)
    • fluentPut

      public JSONObject fluentPut(Object key, Object value)
    • fluentPut

      public JSONObject fluentPut(Object key, Object value, JsonConfig config)
    • putAll

      public void putAll(Map map)
      指定者:
      putAll 在接口中 Map
    • putAll

      public void putAll(Map map, JsonConfig jsonConfig)
    • remove

      public Object remove(Object key)
      指定者:
      remove 在接口中 Map
    • remove

      public Object remove(String key)
      Remove a name and its value, if present.
      参数:
      key - The name to be removed.
      返回:
      The value that was associated with the name, or null if there was no value.
    • size

      public int size()
      Get the number of keys stored in the JSONObject.
      指定者:
      size 在接口中 JSON
      指定者:
      size 在接口中 Map
      返回:
      The number of keys in the JSONObject.
    • toJSONArray

      public JSONArray toJSONArray(JSONArray names)
      Produce a JSONArray containing the values of the members of this JSONObject.
      参数:
      names - A JSONArray containing a list of key strings. This determines the sequence of the values in the result.
      返回:
      A JSONArray of values.
      抛出:
      JSONException - If any of the values are non-finite numbers.
    • toString

      public String toString()
      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.

      Warning: This method assumes that the data structure is acyclical.

      覆盖:
      toString 在类中 Object
      返回:
      a printable, displayable, portable, transmittable representation of the object, beginning with { (left brace) and ending with } (right brace).
    • toJSONString

      public String toJSONString()
    • toString

      public String toString(int indentFactor)
      Make a prettyprinted JSON text of this JSONObject.

      Warning: This method assumes that the data structure is acyclical.

      指定者:
      toString 在接口中 JSON
      参数:
      indentFactor - The number of spaces to add to each level of indentation.
      返回:
      a printable, displayable, portable, transmittable representation of the object, beginning with { (left brace) and ending with } (right brace).
      抛出:
      JSONException - If the object contains an invalid number.
    • toString

      public String toString(int indentFactor, int indent)
      Make a prettyprinted JSON text of this JSONObject.

      Warning: This method assumes that the data structure is acyclical.

      指定者:
      toString 在接口中 JSON
      参数:
      indentFactor - The number of spaces to add to each level of indentation.
      indent - The indentation of the top level.
      返回:
      a printable, displayable, transmittable representation of the object, beginning with { (left brace) and ending with } (right brace).
      抛出:
      JSONException - If the object contains an invalid number.
    • values

      public Collection values()
      指定者:
      values 在接口中 Map
    • write

      public Writer write(Writer writer)
      Write the contents of the JSONObject as JSON text to a writer. For compactness, no whitespace is added.

      Warning: This method assumes that the data structure is acyclical.

      指定者:
      write 在接口中 JSON
      返回:
      The writer.
      抛出:
      JSONException
    • _processValue

      protected Object _processValue(Object value, JsonConfig jsonConfig)
      覆盖:
      _processValue 在类中 AbstractJSON