public class JSONObject extends Object
Constructor and Description |
---|
JSONObject()
Construct a new, empty JSON object.
|
JSONObject(JSONObject original)
Construct a JSON object by copying another pre-existing JSON object.
|
JSONObject(Map<String,Object> map)
Construct a JSON object from a pre-existing map.
|
JSONObject(String type)
Construct a JSON object representing a typed struct.
|
JSONObject(String target,
String verb)
Construct a JSON object representing a message.
|
Modifier and Type | Method and Description |
---|---|
void |
addProperty(String name,
boolean value)
Add a boolean property to the object.
|
void |
addProperty(String name,
double value)
Add a double property to the object.
|
void |
addProperty(String name,
Encodable value)
Add an object that can be encoded as JSON to an object.
|
void |
addProperty(String name,
int value)
Add an integer property to the object.
|
void |
addProperty(String name,
long value)
Add a long property to the object.
|
void |
addProperty(String name,
Object value)
Add a property to the object.
|
void |
copyProperty(String name,
JSONObject orig)
Add a property to the object by copying a property of another object.
|
JSONArray |
getArray(String name)
Obtain an array property value.
|
boolean |
getBoolean(String name)
Obtain the boolean value of a property.
|
double |
getDouble(String name)
Obtain a double property value.
|
int |
getInt(String name)
Obtain an integer property value.
|
long |
getLong(String name)
Obtain a long property value.
|
JSONObject |
getObject(String name)
Obtain a JSON object property value.
|
Object |
getProperty(String name)
Obtain the value of a property.
|
String |
getString(String name)
Obtain a string property value.
|
JSONLiteral |
literal(EncodeControl control)
Convert this JSONObject into a JSONLiteral.
|
JSONArray |
optArray(String name)
Obtain an array property value or an empty array if the property has no
value.
|
JSONArray |
optArray(String name,
JSONArray defaultValue)
Obtain an array property value or a default value if the property has no
value.
|
boolean |
optBoolean(String name,
boolean defaultValue)
Obtain the boolean value of a property or a default value if the
property has no value.
|
double |
optDouble(String name,
double defaultValue)
Obtain the double value of a property or a default value if the property
has no value.
|
int |
optInt(String name,
int defaultValue)
Obtain the integer value of a property or a default value if the
property has no value.
|
long |
optLong(String name,
long defaultValue)
Obtain the long value of a property or a default value if the property
has no value.
|
JSONObject |
optObject(String name)
Obtain the JSON object value of a property or an empty object if the
property has no value.
|
JSONObject |
optObject(String name,
JSONObject defaultValue)
Obtain the JSON object value of a property or a default value if the
property has no value.
|
String |
optString(String name,
String defaultValue)
Obtain the string value of a property or a default value if the
property has no value.
|
static JSONObject |
parse(String str)
Create a JSON object by parsing a JSON object literal string.
|
Set<Map.Entry<String,Object>> |
properties()
Get a set view of the properties of this JSON object.
|
Object |
remove(String name)
Remove a property from this JSON object.
|
String |
sendableString()
Obtain a
String representation of this object suitable for
output to a connection. |
int |
size()
Return the number of properties in this JSON object.
|
String |
target()
Interpreting this JSON object as a JSON message, obtain its target.
|
String |
toString()
Obtain a printable string representation of this object.
|
String |
type()
Interpreting this JSON object as an encoded object descriptor, obtain
its type name.
|
String |
verb()
Interpreting this JSON object as a JSON message, obtain its verb.
|
public JSONObject()
public JSONObject(Map<String,Object> map)
public JSONObject(JSONObject original)
original
- The original JSON object to be copied.public JSONObject(String type)
type
- The type name (this will be added as the property
'type:').public void addProperty(String name, boolean value)
name
- The name of the property to add.value
- Its (boolean) value.public void addProperty(String name, double value)
name
- The name of the property to add.value
- Its (double) value.public void addProperty(String name, int value)
name
- The name of the property to add.value
- Its (integer) value.public void addProperty(String name, long value)
name
- The name of the property to add.value
- Its (integer) value.public void addProperty(String name, Encodable value)
name
- The name of the property to add.value
- Its (Encodable) value.public void copyProperty(String name, JSONObject orig)
name
- The name of the property to copy.orig
- The original object to copy from.public JSONArray getArray(String name) throws JSONDecodingException
name
- The name of the array property sought.JSONDecodingException
- if no value is associated with 'name' or
if the value is not an array.public boolean getBoolean(String name) throws JSONDecodingException
name
- The name of the boolean property sought.JSONDecodingException
- if no value is associated with 'name' or
if the value is not boolean.public double getDouble(String name) throws JSONDecodingException
name
- The name of the double property sought.JSONDecodingException
- if no value is associated with 'name' or
if the value is not a number.public int getInt(String name) throws JSONDecodingException
name
- The name of the integer property sought.JSONDecodingException
- if no value is associated with 'name' or
if the value is not a number.public long getLong(String name) throws JSONDecodingException
name
- The name of the long property sought.JSONDecodingException
- if no value is associated with 'name' or
if the value is not a number.public Object getProperty(String name)
name
- The name of the property soughtpublic JSONObject getObject(String name) throws JSONDecodingException
name
- The name of the object property sought.JSONDecodingException
- if no value is associated with 'name' or
if the value is not a JSON object.public String getString(String name) throws JSONDecodingException
name
- The name of the string property sought.JSONDecodingException
- if no value is associated with 'name' if
the value is not a string.public JSONLiteral literal(EncodeControl control)
control
- Encode control determining what flavor of encoding
is being done.public JSONArray optArray(String name) throws JSONDecodingException
name
- The name of the array property sought.JSONDecodingException
- if property has a value but the value is
not an array.public JSONArray optArray(String name, JSONArray defaultValue) throws JSONDecodingException
name
- The name of the array property sought.defaultValue
- The value to return if there is no such property.JSONDecodingException
- if property has a value but the value is
not an array.public boolean optBoolean(String name, boolean defaultValue) throws JSONDecodingException
name
- The name of the boolean property sought.defaultValue
- The value to return if there is no such property.JSONDecodingException
- if the property has a value but the value
is not boolean.public double optDouble(String name, double defaultValue) throws JSONDecodingException
name
- The name of the double property sought.defaultValue
- The value to return if there is no such property.JSONDecodingException
- if the property has a value but the value
is not a number.public int optInt(String name, int defaultValue) throws JSONDecodingException
name
- The name of the integer property sought.defaultValue
- The value to return if there is no such property.JSONDecodingException
- if the property has a value but the value
is not a number.public long optLong(String name, long defaultValue) throws JSONDecodingException
name
- The name of the long property sought.defaultValue
- The value to return if there is no such property.JSONDecodingException
- if the property has a value but the value
is not a number.public JSONObject optObject(String name, JSONObject defaultValue) throws JSONDecodingException
name
- The name of the JSON object property sought.defaultValue
- The value to return if there is no such property.JSONDecodingException
- if the property has a value but the value
is not a JSONObject
.public JSONObject optObject(String name) throws JSONDecodingException
name
- The name of the JSON object property sought.JSONDecodingException
- if the property has a value but the value
is not a JSONObject
.public String optString(String name, String defaultValue) throws JSONDecodingException
name
- The name of the string property sought.defaultValue
- The value to return if there is no such property.JSONDecodingException
- if the property has a value but the value
is not a String
.public static JSONObject parse(String str) throws SyntaxError
Parser
instead.str
- A JSON literal string that will be parsed and turned into
the corresponding JSONObject.SyntaxError
public Set<Map.Entry<String,Object>> properties()
public Object remove(String name)
name
- The name of the property to remove.public String sendableString()
String
representation of this object suitable for
output to a connection.String
representation of this objectpublic int size()
public String target()
public String toString()
public String type()
public String verb()
Copyright © 2017. All rights reserved.