public class JSONObject
extends Object
get
and opt
methods for accessing the
values by name, and put
methods for adding or replacing values
by name. The values can be any of these types: Boolean
,
JSONArray
, JSONObject
, Number
,
String
, or the JSONObject.NULL
object. A JSONObject
constructor can be used to convert an external form JSON text into an
internal form whose values can be retrieved with the get
and
opt
methods, or to convert values into a JSON text using the
put
and toString
methods. A get
method
returns a value if one can be found, and throws an exception if one cannot be
found. An opt
method returns a default value instead of throwing
an exception, and so is useful for obtaining optional values.
The generic get()
and opt()
methods return an
object, which you can cast or query for type. There are also typed
get
and opt
methods that do type checking and type
coercion for you. The opt methods differ from the get methods in that they do
not throw. Instead, they return a specified value, such as null.
The put
methods add or replace values in an object. For example,
myString = new JSONObject().put("JSON", "Hello, World!").toString();produces the string
{"JSON": "Hello, World"}
.
The texts produced by the toString
methods strictly conform to
the JSON syntax rules. The constructors are more forgiving in the texts they
will accept:
,
(comma) may appear just
before the closing brace.'
(single
quote).{ } [ ] / \ : , = ; #
and if they do not look like numbers and
if they are not the reserved words true
, false
, or
null
.=
or =>
as well as by
:
.;
(semicolon) as
well as by ,
(comma).Modifier and Type | Field and Description |
---|---|
static Object |
NULL
It is sometimes more convenient and less ambiguous to have a
NULL object than to use Java's null value. |
Constructor and Description |
---|
JSONObject()
Construct an empty JSONObject.
|
JSONObject(FloatDict dict) |
JSONObject(IntDict dict) |
JSONObject(Reader reader) |
JSONObject(StringDict dict) |
Modifier and Type | Method and Description |
---|---|
String |
format(int indentFactor)
Make a prettyprinted JSON text of this JSONObject.
|
Object |
get(String key)
Get the value object associated with a key.
|
boolean |
getBoolean(String key)
Get the boolean value associated with a key.
|
boolean |
getBoolean(String key,
boolean defaultValue)
Get an optional boolean associated with a key.
|
double |
getDouble(String key)
Get the double value associated with a key.
|
double |
getDouble(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.
|
float |
getFloat(String key) |
float |
getFloat(String key,
float defaultValue) |
int |
getInt(String key)
Gets the int value associated with a key
|
int |
getInt(String key,
int defaultValue)
Get an optional int value associated with a key,
or the default if there is no such key or if the value is not a number.
|
JSONArray |
getJSONArray(String key)
Get the JSONArray value associated with a key.
|
JSONObject |
getJSONObject(String key)
Get the JSONObject value associated with a key.
|
long |
getLong(String key)
Get the long value associated with a key.
|
long |
getLong(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.
|
String |
getString(String key)
Gets the String associated with a key
|
String |
getString(String key,
String defaultValue)
Get an optional string associated with a key.
|
boolean |
hasKey(String key)
Determine if the JSONObject contains a specific key.
|
boolean |
isNull(String key)
Determine if the value associated with the key is null or if there is
no value.
|
Iterator |
keyIterator()
Get an enumeration of the keys of the JSONObject.
|
Set |
keys()
Get a set of keys of the JSONObject.
|
static JSONObject |
parse(String source)
Construct a JSONObject from a source JSON text string.
|
JSONObject |
put(String key,
Object value)
Put a key/value pair in the JSONObject.
|
static String |
quote(String string)
Produce a string in double quotes with backslash sequences in all the
right places.
|
static Writer |
quote(String string,
Writer w) |
Object |
remove(String key)
Remove a name and its value, if present.
|
boolean |
save(File file,
String options) |
JSONObject |
setBoolean(String key,
boolean value)
Put a key/boolean pair in the JSONObject.
|
JSONObject |
setDouble(String key,
double value)
Put a key/double pair in the JSONObject.
|
JSONObject |
setFloat(String key,
float value) |
JSONObject |
setInt(String key,
int value)
Put a key/int pair in the JSONObject.
|
JSONObject |
setJSONArray(String key,
JSONArray value) |
JSONObject |
setJSONObject(String key,
JSONObject value) |
JSONObject |
setLong(String key,
long value)
Put a key/long pair in the JSONObject.
|
JSONObject |
setString(String key,
String value) |
int |
size()
Get the number of keys stored in the JSONObject.
|
String |
toString()
Return the JSON data formatted with two spaces for indents.
|
boolean |
write(PrintWriter output) |
boolean |
write(PrintWriter output,
String options) |
public static final Object NULL
NULL
object than to use Java's null
value.
JSONObject.NULL.equals(null)
returns true
.
JSONObject.NULL.toString()
returns "null"
.public JSONObject()
public JSONObject(Reader reader)
public JSONObject(IntDict dict)
public JSONObject(FloatDict dict)
public JSONObject(StringDict dict)
public static JSONObject parse(String source)
source
- A string beginning
with {
(left brace) and ending
with }
(right brace).RuntimeException
- If there is a syntax error in the source
string or a duplicated key.public Object get(String key)
key
- A key string.RuntimeException
- if the key is not found.public String getString(String key)
key
- a key stringRuntimeException
- if there is no string value for the key.getInt(String)
,
getFloat(String)
,
getBoolean(String)
public String getString(String key, String defaultValue)
key
- A key string.defaultValue
- The default.public int getInt(String key)
key
- A key string.RuntimeException
- if the key is not found or if the value cannot
be converted to an integer.getFloat(String)
,
getString(String)
,
getBoolean(String)
public int getInt(String key, int defaultValue)
key
- A key string.defaultValue
- The default.public long getLong(String key)
key
- A key string.RuntimeException
- if the key is not found or if the value cannot
be converted to a long.public long getLong(String key, long defaultValue)
key
- A key string.defaultValue
- The default.public float getFloat(String key)
key
- a key stringgetInt(String)
,
getString(String)
,
getBoolean(String)
public float getFloat(String key, float defaultValue)
public double getDouble(String key)
key
- A key string.RuntimeException
- if the key is not found or
if the value is not a Number object and cannot be converted to a number.public double getDouble(String key, double defaultValue)
key
- A key string.defaultValue
- The default.public boolean getBoolean(String key)
key
- a key stringRuntimeException
- if the value is not a Boolean or the String "true" or "false".getInt(String)
,
getFloat(String)
,
getString(String)
public boolean getBoolean(String key, boolean defaultValue)
key
- A key string.defaultValue
- The default.public JSONArray getJSONArray(String key)
key
- a key stringRuntimeException
- if the value is not a JSONArray.getJSONObject(String)
,
setJSONObject(String, JSONObject)
,
setJSONArray(String, JSONArray)
public JSONObject getJSONObject(String key)
key
- a key stringRuntimeException
- if the value is not a JSONObject.getJSONArray(String)
,
setJSONObject(String, JSONObject)
,
setJSONArray(String, JSONArray)
public boolean hasKey(String key)
key
- A key string.public boolean isNull(String key)
key
- A key string.public Iterator keyIterator()
public Set keys()
public int size()
public JSONObject setString(String key, String value)
key
- a key stringvalue
- the value to assignsetInt(String, int)
,
setFloat(String, float)
,
setBoolean(String, boolean)
public JSONObject setInt(String key, int value)
key
- a key stringvalue
- the value to assignRuntimeException
- If the key is null.setFloat(String, float)
,
setString(String, String)
,
setBoolean(String, boolean)
public JSONObject setLong(String key, long value)
key
- A key string.value
- A long which is the value.RuntimeException
- If the key is null.public JSONObject setFloat(String key, float value)
key
- a key stringvalue
- the value to assignRuntimeException
- If the key is null or if the number is NaN or infinite.setInt(String, int)
,
setString(String, String)
,
setBoolean(String, boolean)
public JSONObject setDouble(String key, double value)
key
- A key string.value
- A double which is the value.RuntimeException
- If the key is null or if the number is NaN or infinite.public JSONObject setBoolean(String key, boolean value)
key
- a key stringvalue
- the value to assignRuntimeException
- If the key is null.setInt(String, int)
,
setFloat(String, float)
,
setString(String, String)
public JSONObject setJSONObject(String key, JSONObject value)
key
- a key stringvalue
- value to assignsetJSONArray(String, JSONArray)
,
getJSONObject(String)
,
getJSONArray(String)
public JSONObject setJSONArray(String key, JSONArray value)
key
- a key stringvalue
- value to assignsetJSONObject(String, JSONObject)
,
getJSONObject(String)
,
getJSONArray(String)
public JSONObject put(String key, Object value)
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 JSONObject.NULL object.RuntimeException
- If the value is non-finite number
or if the key is null.public static String quote(String string)
string
- A Stringpublic static Writer quote(String string, Writer w) throws IOException
IOException
public Object remove(String key)
key
- The name to be removed.public boolean save(File file, String options)
public boolean write(PrintWriter output)
public boolean write(PrintWriter output, String options)
public String toString()
toString
in class Object
public String format(int indentFactor)
Warning: This method assumes that the data structure is acyclical.
indentFactor
- The number of spaces to add to each level of
indentation.{
(left brace) and ending
with }
(right brace).RuntimeException
- If the object contains an invalid number.