public final class JSONObject extends JSONCollection
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
, JSONLiteral
, 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 coersion for you.
The put
methods adds values to 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).0-
(octal) or 0x-
(hex) prefix.JSONLiteral
type has been added, which allows the exact
output to be controlled; useful when a JSONObject is being used as a configuration object, and must contain values
that are not simple data, such as an inline function (making the result not JSON).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(JSONObject source,
String... propertyNames)
Construct a JSONObject from a subset of another JSONObject.
|
JSONObject(Object... keysAndValues)
Constructs a new JSONObject using a series of String keys and object values.
|
JSONObject(String string)
Construct a JSONObject from a string.
|
Modifier and Type | Method and Description |
---|---|
JSONObject |
accumulate(String key,
Object value)
Accumulate values under a key.
|
JSONObject |
append(String key,
Object value)
Append values to the array under a key.
|
JSONObject |
copy()
Returns a new JSONObject that is a shallow copy of this JSONObject.
|
boolean |
equals(Object obj)
Returns true if the other object is a JSONObject and its set of properties matches this object's properties.
|
Object |
get(String key)
Get the value object associated with a key.
|
boolean |
getBoolean(String key)
Get the boolean value associated with a key.
|
double |
getDouble(String key)
Get the double value associated with a key.
|
int |
getInt(String key)
Get the int value associated with a key.
|
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.
|
String |
getString(String key)
Get the string associated with a key.
|
boolean |
has(String key)
Determine if the JSONObject contains a specific key.
|
JSONObject |
in(String key)
Navigates into a nested JSONObject, creating the JSONObject if necessary.
|
boolean |
isNull(String key)
Determine if the value associated with the key is null or if there is no value.
|
Set<String> |
keys()
Get an enumeration of the keys of the JSONObject.
|
int |
length()
Get the number of keys stored in the JSONObject.
|
JSONArray |
names()
Produce a JSONArray containing the names of the elements of this JSONObject.
|
Object |
opt(String key)
Get an optional value associated with a key.
|
JSONObject |
put(String key,
Object value)
Put a key/value pair in the JSONObject.
|
JSONObject |
putAll(Map<String,?> newProperties)
Invokes
put(String, Object) for each value from the map. |
static String |
quote(String string)
Produce a string in double quotes with backslash sequences in all the right places,
allowing JSON text to be delivered in HTML.
|
Object |
remove(String key)
Remove a name and its value, if present.
|
Map<String,Object> |
toMap()
Returns a Map of the keys and values of the JSONObject.
|
prettyPrint, print, print, toCompactString, toString, toString
public JSONObject()
public JSONObject(Object... keysAndValues)
put(String, Object)
. Keys must be strings
(toString() will be invoked on each key).
Prior to release 5.4, keysAndValues was type String...; changing it to Object... makes
it much easier to initialize a JSONObject in a single statement, which is more readable.public JSONObject(JSONObject source, String... propertyNames)
source
- A JSONObject.propertyNames
- The strings to copy.RuntimeException
- If a value is a non-finite number.public JSONObject(String string)
string
- A string beginning with {
(left brace) and ending with }
(right brace).RuntimeException
- If there is a syntax error in the source string.public JSONObject copy()
public JSONObject accumulate(String key, Object value)
key
- A key string.value
- An object to be accumulated under the key.RuntimeException
- if the value is an invalid number or if the key is null.public JSONObject append(String key, Object value)
key
- A key string.value
- An object to be accumulated under the key.public Object get(String key)
key
- A key string.RuntimeException
- if the key is not found.opt(String)
public boolean getBoolean(String key)
key
- A key string.RuntimeException
- if the value does not exist, is not a Boolean or the String "true" or "false".public double getDouble(String key)
key
- A key string.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.public JSONArray getJSONArray(String key)
key
- A key string.RuntimeException
- if the key is not found or if the value is not a JSONArray.public JSONObject getJSONObject(String key)
key
- A key string.RuntimeException
- if the key is not found or if the value is not a JSONObject.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 String getString(String key)
key
- A key string.RuntimeException
- if the key is not found.public boolean has(String key)
key
- A key string.public boolean isNull(String key)
key
- A key string.public Set<String> keys()
public int length()
public JSONArray names()
public Object opt(String key)
key
- A key string.get(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, JSONLiteral, 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 Object remove(String key)
key
- The name to be removed.public boolean equals(Object obj)
public Map<String,Object> toMap()
NULL
in the map.public JSONObject putAll(Map<String,?> newProperties)
put(String, Object)
for each value from the map.newProperties
- to add to this JSONObjectpublic JSONObject in(String key)
key
- IllegalStateException
- if the current value for the key is not null and not JSONObject${project.version} - Copyright © 2003-2015 The Apache Software Foundation.