Package 

Class JSONObject

  • All Implemented Interfaces:
    java.io.Serializable

    
    public class JSONObject
     implements Serializable
                        

    A modifiable set of name/value mappings. Names are unique, non-null strings. Values may be any mix of JSONObjects, JSONArrays, Strings, Booleans, Integers, Longs, Doubles or NULL. Values may not be {@code null}, NaNs, infinities, or of any type not listed here.

    This class can coerce values to another type when requested.

    • When the requested type is a boolean, strings will be coerced using a case-insensitive comparison to "true" and "false".
    • When the requested type is a double, other Number types will be coerced using doubleValue. Strings that can be coerced using valueOf will be.
    • When the requested type is an int, other Number types will be coerced using intValue. Strings that can be coerced using valueOf will be, and then cast to int.
    • When the requested type is a long, other Number types will be coerced using longValue. Strings that can be coerced using valueOf will be, and then cast to long. This two-step conversion is lossy for very large values. For example, the string "9223372036854775806" yields the long 9223372036854775807.
    • When the requested type is a String, other non-null values will be coerced using valueOf. Although null cannot be coerced, the sentinel value NULL is coerced to the string "null".

    This class can look up both mandatory and optional values:

    • Use getType() to retrieve a mandatory value. This fails with a {@code JSONException} if the requested name has no value or if the value cannot be coerced to the requested type.
    • Use optType() to retrieve an optional value. This returns a system- or user-supplied default if the requested name has no value or if the value cannot be coerced to the requested type.

    Warning: this class represents null in two incompatible ways: the standard Java {@code null} reference, and the sentinel value . In particular, calling {@code put(name, null)} removes the named entry from the object but {@code put(name, JSONObject.NULL)} stores an entry whose value is {@code JSONObject.NULL}.

    Instances of this class are not thread safe. Although this class is nonfinal, it was not designed for inheritance and should not be subclassed. In particular, self-use by overrideable methods is not specified. See Effective Java Item 17, "Design and Document or inheritance or else prohibit it" for further information.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      public final static Object NULL
    • Constructor Summary

      Constructors 
      Constructor Description
      JSONObject() Creates a {@code JSONObject} with no name/value mappings.
      JSONObject(Map copyFrom) Creates a new {@code JSONObject} by copying all name/value mappings fromthe given map.
      JSONObject(JSONTokener readFrom) Creates a new {@code JSONObject} with name/value mappings from the nextobject in the tokener.
      JSONObject(String json) Creates a new {@code JSONObject} with name/value mappings from the JSONstring.
      JSONObject(JSONObject copyFrom, Array<String> names) Creates a new {@code JSONObject} by copying mappings for the listed namesfrom the given object.
      JSONObject(JSONObject copyFrom) Creates a new {@code JSONObject} by copying mappings for given object
    • Method Summary

      Modifier and Type Method Description
      int length() Returns the number of name/value mappings in this object.
      JSONObject put(String name, boolean value) Maps {@code name} to {@code value}, clobbering any existing name/valuemapping with the same name.
      JSONObject put(String name, double value) Maps {@code name} to {@code value}, clobbering any existing name/valuemapping with the same name.
      JSONObject put(String name, int value) Maps {@code name} to {@code value}, clobbering any existing name/valuemapping with the same name.
      JSONObject put(String name, long value) Maps {@code name} to {@code value}, clobbering any existing name/valuemapping with the same name.
      JSONObject put(String name, Object value) Maps {@code name} to {@code value}, clobbering any existing name/valuemapping with the same name.
      JSONObject putOpt(String name, Object value) Equivalent to {@code put(name, value)} when both parameters are non-null;does nothing otherwise.
      JSONObject accumulate(String name, Object value) Appends {@code value} to the array already mapped to {@code name}.
      JSONObject append(String name, Object value) Appends values to the array mapped to {@code name}.
      Object remove(String name) Removes the named mapping if it exists; does nothing otherwise.
      boolean isNull(String name) Returns true if this object has no mapping for {@code name} or if it hasa mapping whose value is NULL.
      boolean has(String name) Returns true if this object has a mapping for {@code name}.
      boolean hasNonNull(String name) Returns true if this object has a mapping for {@code name}.
      Object get(String name) Returns the value mapped by {@code name}, or throws if no such mapping exists.
      Object opt(String name) Returns the value mapped by {@code name}, or null if no such mappingexists.
      boolean getBoolean(String name) Returns the value mapped by {@code name} if it exists and is a boolean orcan be coerced to a boolean, or throws otherwise.
      boolean optBoolean(String name) Returns the value mapped by {@code name} if it exists and is a boolean orcan be coerced to a boolean, or false otherwise.
      boolean optBoolean(String name, boolean fallback) Returns the value mapped by {@code name} if it exists and is a boolean orcan be coerced to a boolean, or {@code fallback} otherwise.
      Boolean reallyOptBoolean(String name, Boolean fallback) Returns the value mapped by {@code name} if it exists and is not null, coercing it ifnecessary, or {@code fallback} if no such mapping exists or is null.
      double getDouble(String name) Returns the value mapped by {@code name} if it exists and is a double orcan be coerced to a double, or throws otherwise.
      double optDouble(String name) Returns the value mapped by {@code name} if it exists and is a double orcan be coerced to a double, or {@code NaN} otherwise.
      double optDouble(String name, double fallback) Returns the value mapped by {@code name} if it exists and is a double orcan be coerced to a double, or {@code fallback} otherwise.
      Double reallyOptDouble(String name, Double fallback) Returns the value mapped by {@code name} if it exists and is not null, coercing it ifnecessary, or {@code fallback} if no such mapping exists or is null.
      int getInt(String name) Returns the value mapped by {@code name} if it exists and is an int orcan be coerced to an int, or throws otherwise.
      int optInt(String name) Returns the value mapped by {@code name} if it exists and is an int orcan be coerced to an int, or 0 otherwise.
      int optInt(String name, int fallback) Returns the value mapped by {@code name} if it exists and is an int orcan be coerced to an int, or {@code fallback} otherwise.
      Integer reallyOptInteger(String name, Integer fallback) Returns the value mapped by {@code name} if it exists and is not null, coercing it ifnecessary, or {@code fallback} if no such mapping exists or is null.
      long getLong(String name) Returns the value mapped by {@code name} if it exists and is a long orcan be coerced to a long, or throws otherwise.
      Long reallyOptLong(String name, Long fallback) Returns the value mapped by {@code name} if it exists and is not null, coercing it ifnecessary, or {@code fallback} if no such mapping exists or is null.
      long optLong(String name) Returns the value mapped by {@code name} if it exists and is a long orcan be coerced to a long, or 0 otherwise.
      long optLong(String name, long fallback) Returns the value mapped by {@code name} if it exists and is a long orcan be coerced to a long, or {@code fallback} otherwise.
      String getString(String name) Returns the value mapped by {@code name} if it exists, coercing it ifnecessary, or throws if no such mapping exists.
      String optString(String name) Returns the value mapped by {@code name} if it exists, coercing it ifnecessary, or the empty string if no such mapping exists.
      String optString(String name, String fallback) Returns the value mapped by {@code name} if it exists, coercing it ifnecessary, or {@code fallback} if no such mapping exists.
      String reallyOptString(String name, String fallback) Returns the value mapped by {@code name} if it exists and is not null, coercing it ifnecessary, or {@code fallback} if no such mapping exists or is null.
      JSONArray getJSONArray(String name) Returns the value mapped by {@code name} if it exists and is a {@code * JSONArray}, or throws otherwise.
      JSONArray optJSONArray(String name) Returns the value mapped by {@code name} if it exists and is a {@code * JSONArray}, or null otherwise.
      JSONObject getJSONObject(String name) Returns the value mapped by {@code name} if it exists and is a {@code * JSONObject}, or throws otherwise.
      JSONObject optJSONObject(String name) Returns the value mapped by {@code name} if it exists and is a {@code * JSONObject}, or null otherwise.
      JSONArray toJSONArray(JSONArray names) Returns an array with the values corresponding to {@code names}.
      Iterator<String> keys() Returns an iterator of the {@code String} names in this object.
      Set<String> keySet() Returns the set of {@code String} names in this object.
      JSONArray names() Returns an array containing the string names in this object.
      String toString() Encodes this object as a compact JSON string, such as:
      {"query":"Pizza","locations":[94043,90210]}
      String toString(int indentSpaces) Encodes this object as a human readable JSON string for debugging, suchas:
      {
          "query": "Pizza",
          "locations": [
              94043,
              90210
          ]
      }
      static String numberToString(Number number) Encodes the number as a JSON string.
      static String quote(String data) Encodes {@code data} as a JSON string.
      static Object wrap(Object o) Wraps the given object if necessary.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • JSONObject

        JSONObject()
        Creates a {@code JSONObject} with no name/value mappings.
      • JSONObject

        JSONObject(Map copyFrom)
        Creates a new {@code JSONObject} by copying all name/value mappings fromthe given map.
        Parameters:
        copyFrom - a map whose keys are of type String and whosevalues are of supported types.
      • JSONObject

        JSONObject(JSONTokener readFrom)
        Creates a new {@code JSONObject} with name/value mappings from the nextobject in the tokener.
        Parameters:
        readFrom - a tokener whose nextValue() method will yield a{@code JSONObject}.
      • JSONObject

        JSONObject(String json)
        Creates a new {@code JSONObject} with name/value mappings from the JSONstring.
        Parameters:
        json - a JSON-encoded string containing an object.
      • JSONObject

        JSONObject(JSONObject copyFrom, Array<String> names)
        Creates a new {@code JSONObject} by copying mappings for the listed namesfrom the given object.
      • JSONObject

        JSONObject(JSONObject copyFrom)
        Creates a new {@code JSONObject} by copying mappings for given object
    • Method Detail

      • length

         int length()

        Returns the number of name/value mappings in this object.

      • put

         JSONObject put(String name, boolean value)

        Maps {@code name} to {@code value}, clobbering any existing name/valuemapping with the same name.

      • put

         JSONObject put(String name, double value)

        Maps {@code name} to {@code value}, clobbering any existing name/valuemapping with the same name.

        Parameters:
        value - a finite value.
      • put

         JSONObject put(String name, int value)

        Maps {@code name} to {@code value}, clobbering any existing name/valuemapping with the same name.

      • put

         JSONObject put(String name, long value)

        Maps {@code name} to {@code value}, clobbering any existing name/valuemapping with the same name.

      • put

         JSONObject put(String name, Object value)

        Maps {@code name} to {@code value}, clobbering any existing name/valuemapping with the same name. If the value is {@code null}, any existingmapping for {@code name} is removed.

        Parameters:
        value - a JSONObject, JSONArray, String, Boolean,Integer, Long, Double, NULL, or {@code null}.
      • putOpt

         JSONObject putOpt(String name, Object value)

        Equivalent to {@code put(name, value)} when both parameters are non-null;does nothing otherwise.

      • accumulate

         JSONObject accumulate(String name, Object value)

        Appends {@code value} to the array already mapped to {@code name}. Ifthis object has no mapping for {@code name}, this inserts a new mapping.If the mapping exists but its value is not an array, the existingand new values are inserted in order into a new array which is itselfmapped to {@code name}. In aggregate, this allows values to be added to amapping one at a time.

        Note that {@code append(String, Object)} provides better semantics.In particular, the mapping for {@code name} will always be a JSONArray. Using {@code accumulate} will result in either a JSONArray or a mapping whose type is the type of {@code value} depending on the number of calls to it.

        Parameters:
        value - a JSONObject, JSONArray, String, Boolean,Integer, Long, Double, NULL or null.
      • remove

         Object remove(String name)

        Removes the named mapping if it exists; does nothing otherwise.

      • isNull

         boolean isNull(String name)

        Returns true if this object has no mapping for {@code name} or if it hasa mapping whose value is NULL.

      • has

         boolean has(String name)

        Returns true if this object has a mapping for {@code name}. The mappingmay be NULL.

      • hasNonNull

         boolean hasNonNull(String name)

        Returns true if this object has a mapping for {@code name}. The mappingCANNOT be NULL.

      • get

         Object get(String name)

        Returns the value mapped by {@code name}, or throws if no such mapping exists.

      • opt

         Object opt(String name)

        Returns the value mapped by {@code name}, or null if no such mappingexists.

      • getBoolean

         boolean getBoolean(String name)

        Returns the value mapped by {@code name} if it exists and is a boolean orcan be coerced to a boolean, or throws otherwise.

      • optBoolean

         boolean optBoolean(String name)

        Returns the value mapped by {@code name} if it exists and is a boolean orcan be coerced to a boolean, or false otherwise.

      • optBoolean

         boolean optBoolean(String name, boolean fallback)

        Returns the value mapped by {@code name} if it exists and is a boolean orcan be coerced to a boolean, or {@code fallback} otherwise.

      • reallyOptBoolean

         Boolean reallyOptBoolean(String name, Boolean fallback)

        Returns the value mapped by {@code name} if it exists and is not null, coercing it ifnecessary, or {@code fallback} if no such mapping exists or is null.

      • getDouble

         double getDouble(String name)

        Returns the value mapped by {@code name} if it exists and is a double orcan be coerced to a double, or throws otherwise.

      • optDouble

         double optDouble(String name)

        Returns the value mapped by {@code name} if it exists and is a double orcan be coerced to a double, or {@code NaN} otherwise.

      • optDouble

         double optDouble(String name, double fallback)

        Returns the value mapped by {@code name} if it exists and is a double orcan be coerced to a double, or {@code fallback} otherwise.

      • reallyOptDouble

         Double reallyOptDouble(String name, Double fallback)

        Returns the value mapped by {@code name} if it exists and is not null, coercing it ifnecessary, or {@code fallback} if no such mapping exists or is null.

      • getInt

         int getInt(String name)

        Returns the value mapped by {@code name} if it exists and is an int orcan be coerced to an int, or throws otherwise.

      • optInt

         int optInt(String name)

        Returns the value mapped by {@code name} if it exists and is an int orcan be coerced to an int, or 0 otherwise.

      • optInt

         int optInt(String name, int fallback)

        Returns the value mapped by {@code name} if it exists and is an int orcan be coerced to an int, or {@code fallback} otherwise.

      • reallyOptInteger

         Integer reallyOptInteger(String name, Integer fallback)

        Returns the value mapped by {@code name} if it exists and is not null, coercing it ifnecessary, or {@code fallback} if no such mapping exists or is null.

      • getLong

         long getLong(String name)

        Returns the value mapped by {@code name} if it exists and is a long orcan be coerced to a long, or throws otherwise.Note that JSON represents numbers as doubles,so this is lossy; use strings to transfer numbers via JSON.

      • reallyOptLong

         Long reallyOptLong(String name, Long fallback)

        Returns the value mapped by {@code name} if it exists and is not null, coercing it ifnecessary, or {@code fallback} if no such mapping exists or is null.

      • optLong

         long optLong(String name)

        Returns the value mapped by {@code name} if it exists and is a long orcan be coerced to a long, or 0 otherwise. Note that JSON represents numbers as doubles,so this is lossy; use strings to transfer numbers via JSON.

      • optLong

         long optLong(String name, long fallback)

        Returns the value mapped by {@code name} if it exists and is a long orcan be coerced to a long, or {@code fallback} otherwise. Note that JSON representsnumbers as doubles, so this is lossy; use strings to transfernumbers via JSON.

      • getString

         String getString(String name)

        Returns the value mapped by {@code name} if it exists, coercing it ifnecessary, or throws if no such mapping exists.

      • optString

         String optString(String name)

        Returns the value mapped by {@code name} if it exists, coercing it ifnecessary, or the empty string if no such mapping exists.

      • optString

         String optString(String name, String fallback)

        Returns the value mapped by {@code name} if it exists, coercing it ifnecessary, or {@code fallback} if no such mapping exists.

      • reallyOptString

         String reallyOptString(String name, String fallback)

        Returns the value mapped by {@code name} if it exists and is not null, coercing it ifnecessary, or {@code fallback} if no such mapping exists or is null.

      • getJSONArray

         JSONArray getJSONArray(String name)

        Returns the value mapped by {@code name} if it exists and is a {@code * JSONArray}, or throws otherwise.

      • optJSONArray

         JSONArray optJSONArray(String name)

        Returns the value mapped by {@code name} if it exists and is a {@code * JSONArray}, or null otherwise.

      • getJSONObject

         JSONObject getJSONObject(String name)

        Returns the value mapped by {@code name} if it exists and is a {@code * JSONObject}, or throws otherwise.

      • optJSONObject

         JSONObject optJSONObject(String name)

        Returns the value mapped by {@code name} if it exists and is a {@code * JSONObject}, or null otherwise.

      • toJSONArray

         JSONArray toJSONArray(JSONArray names)

        Returns an array with the values corresponding to {@code names}. Thearray contains null for names that aren't mapped. This method returnsnull if {@code names} is either null or empty.

      • keys

         Iterator<String> keys()

        Returns an iterator of the {@code String} names in this object. Thereturned iterator supports remove, which willremove the corresponding mapping from this object. If this object ismodified after the iterator is returned, the iterator's behavior isundefined. The order of the keys is undefined.

      • keySet

         Set<String> keySet()

        Returns the set of {@code String} names in this object. The returned setis a view of the keys in this object. remove will removethe corresponding mapping from this object and set iterator behaviouris undefined if this object is modified after it is returned.

        See keys.

      • names

         JSONArray names()

        Returns an array containing the string names in this object. This methodreturns null if this object contains no mappings.

      • toString

         String toString()

        Encodes this object as a compact JSON string, such as:

        {"query":"Pizza","locations":[94043,90210]}
      • toString

         String toString(int indentSpaces)

        Encodes this object as a human readable JSON string for debugging, suchas:

        {
            "query": "Pizza",
            "locations": [
                94043,
                90210
            ]
        }
        Parameters:
        indentSpaces - the number of spaces to indent for each level ofnesting.
      • numberToString

         static String numberToString(Number number)

        Encodes the number as a JSON string.

        Parameters:
        number - a finite value.
      • quote

         static String quote(String data)

        Encodes {@code data} as a JSON string. This applies quotes and anynecessary character escaping.

        Parameters:
        data - the string to encode.
      • wrap

         static Object wrap(Object o)

        Wraps the given object if necessary.

        If the object is null or , returns NULL.If the object is a {@code JSONArray} or {@code JSONObject}, no wrapping is necessary.If the object is {@code NULL}, no wrapping is necessary.If the object is an array or {@code Collection}, returns an equivalent {@code JSONArray}.If the object is a {@code Map}, returns an equivalent {@code JSONObject}.If the object is a primitive wrapper type or {@code String}, returns the object.Otherwise if the object is from a {@code java} package, returns the result of {@code toString}.If wrapping fails, returns null.