public class JsonObject extends Object
The constructor can convert an external form string into an internal form Java object. The toString() method creates an external form string.
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 texts produced by the toString() methods are very strict. 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 :; as well as by ,0- (octal) or 0x- (hex) prefix.#| 限定符和类型 | 字段和说明 |
|---|---|
static Object |
NULL
It is sometimes more convenient and less ambiguous to have a NULL object than to use Java's null value.
|
| 构造器和说明 |
|---|
JsonObject()
Construct an empty JSONObject.
|
JsonObject(JsonObject jo,
String[] sa)
Construct a JSONObject from a subset of another JSONObject.
|
JsonObject(JsonTokener x)
Construct a JSONObject from a JSONTokener.
|
JsonObject(Map<String,Object> map)
Construct a JSONObject from a Map.
|
JsonObject(String string)
Construct a JSONObject from a string.
|
| 限定符和类型 | 方法和说明 |
|---|---|
JsonObject |
accumulate(String key,
Object value)
Accumulate values under a key.
|
StringBuilder |
append(StringBuilder sb)
Appends this JsonObject to the provided string builder.
|
protected static StringBuilder |
appendQuoted(String str,
StringBuilder sb)
Appends a string in double quotes with backslash sequences in all the right places.
|
protected static StringBuilder |
appendValue(Object value,
StringBuilder sb)
Appends the given value to the provided string builder.
|
static String |
escape(String string) |
static String |
escape(String str,
boolean singleQuote) |
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.
|
protected static int |
getQuotedSize(String str)
Returns an estimate of the size of the string in the quoted form.
|
String |
getString(String key)
Get the string associated with a key.
|
protected static int |
getValueSize(Object value)
Returns an estimate of the size of the value in a serialized form.
|
boolean |
has(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<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.
|
static String |
numberToString(Number n)
Produce a string from a number.
|
Object |
opt(String key)
Get an optional value associated with a key.
|
boolean |
optBoolean(String key)
Get an optional boolean associated with a key.
|
boolean |
optBoolean(String key,
boolean defaultValue)
Get an optional boolean associated with a key.
|
double |
optDouble(String key)
Get an optional double associated with a key, or NaN if there is no such key or if its value is not a number.
|
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.
|
int |
optInt(String key)
Get an optional int value associated with a key, or zero if there is no such key or if the value is not a number.
|
int |
optInt(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 |
optJSONArray(String key)
Get an optional JSONArray associated with a key.
|
JsonObject |
optJSONObject(String key)
Get an optional JSONObject associated with a key.
|
String |
optString(String key)
Get an optional string associated with a key.
|
String |
optString(String key,
String defaultValue)
Get an optional string associated with a key.
|
JsonObject |
put(String key,
boolean value)
Put a key/boolean pair in the JSONObject.
|
JsonObject |
put(String key,
double value)
Put a key/double pair in the JSONObject.
|
JsonObject |
put(String key,
int value)
Put a key/int pair in the JSONObject.
|
JsonObject |
put(String key,
Object value)
Put a key/value pair in the JSONObject.
|
JsonObject |
putOpt(String key,
Object value)
Put a key/value pair in the JSONObject, but only if the value is non-null.
|
static String |
quote(String str)
Produce a string in double quotes with backslash sequences in all the right places.
|
Object |
remove(String key)
Remove a name and its value, if present.
|
JsonArray |
toJSONArray(JsonArray names)
Produce a JSONArray containing the values of the members of this JSONObject.
|
String |
toString()
Make an JSON external form string of this JSONObject.
|
String |
toString(int indentFactor)
Make a prettyprinted JSON external form string of this JSONObject.
|
public static final Object NULL
public JsonObject()
public JsonObject(JsonObject jo, String[] sa)
jo - A JSONObject.sa - An array of strings.public JsonObject(JsonTokener x) throws ParseException
x - A JSONTokener object containing the source string.ParseException - if there is a syntax error in the source string.public JsonObject(Map<String,Object> map)
map - A map object that can be used to initialize the contents of the JSONObject.public JsonObject(String string) throws ParseException
string - A string beginning with { (left brace) and ending with }
(right brace).ParseException - The string must be properly formatted.public static String numberToString(Number n) throws ArithmeticException
n - A NumberArithmeticException - JSON can only serialize finite numbers.public static String quote(String str)
string - A Stringprotected static StringBuilder appendQuoted(String str, StringBuilder sb)
string - A Stringprotected static int getQuotedSize(String str)
protected static StringBuilder appendValue(Object value, StringBuilder sb)
value - the value to be appended.sb - the string builder to which the value should be appended.protected static int getValueSize(Object value)
public JsonObject accumulate(String key, Object value) throws NullPointerException
key - A key string.value - An object to be accumulated under the key.NullPointerException - if the key is nullpublic Object get(String key) throws NoSuchElementException
key - A key string.NoSuchElementException - if the key is not found.public boolean getBoolean(String key) throws ClassCastException, NoSuchElementException
key - A key string.NoSuchElementException - if the key is not found.ClassCastException - if the value is not a Boolean or the String "true" or "false".public double getDouble(String key) throws NoSuchElementException, NumberFormatException
key - A key string.NumberFormatException - if the value cannot be converted to a number.NoSuchElementException - if the key is not found or if the value is a Number object.public long getLong(String key) throws NoSuchElementException, NumberFormatException
key - A key string.NumberFormatException - if the value cannot be converted to a number.NoSuchElementException - if the key is not found or if the value is a Number object.public int getInt(String key) throws NoSuchElementException, NumberFormatException
key - A key string.NoSuchElementException - if the key is not foundNumberFormatException - if the value cannot be converted to a number.public JsonArray getJSONArray(String key) throws NoSuchElementException
key - A key string.NoSuchElementException - if the key is not found or if the value is not a JSONArray.public JsonObject getJSONObject(String key) throws NoSuchElementException
key - A key string.NoSuchElementException - if the key is not found or if the value is not a JSONObject.public String getString(String key) throws NoSuchElementException
key - A key string.NoSuchElementException - 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 Iterator<String> keys()
public int length()
public JsonArray names()
public Object opt(String key) throws NullPointerException
key - A key string.NullPointerException - The key must not be null.public boolean optBoolean(String key)
key - A key string.public boolean optBoolean(String key, boolean defaultValue)
key - A key string.defaultValue - The default.public double optDouble(String key)
key - A string which is the key.public double optDouble(String key, double defaultValue)
key - A key string.defaultValue - The default.public int optInt(String key)
key - A key string.public int optInt(String key, int defaultValue)
key - A key string.defaultValue - The default.public JsonArray optJSONArray(String key)
key - A key string.public JsonObject optJSONObject(String key)
key - A key string.public String optString(String key)
key - A key string.public String optString(String key, String defaultValue)
key - A key string.defaultValue - The default.public JsonObject put(String key, boolean value)
key - A key string.value - A boolean which is the value.public JsonObject put(String key, double value)
key - A key string.value - A double which is the value.public JsonObject put(String key, int value)
key - A key string.value - An int which is the value.public JsonObject put(String key, Object value) throws NullPointerException
key - A key string.value - An object which is the value. It should be of one of these types: Boolean, Double, Integer, JSONArray, JSONObject,
String, or the JSONObject.NULL object.NullPointerException - The key must be non-null.public JsonObject putOpt(String key, Object value) throws NullPointerException
key - A key string.value - An object which is the value. It should be of one of these types: Boolean, Double, Integer, JSONArray, JSONObject,
String, or the JSONObject.NULL object.NullPointerException - The key must be non-null.public Object remove(String key)
key - The name to be removed.public JsonArray toJSONArray(JsonArray names)
names - A JSONArray containing a list of key strings. This determines the sequence of the values in the result.public String toString()
Warning: This method assumes that the data structure is acyclical.
Warning: whenever toString() is overridden, you must override append() as well. Not overriding append() will result in incorrect serialization results. The append() method should contain the actual concatenation logic and toString() should simply utilize append(). An example of overriding both methods is as follows:
@Override
public String toString() {
return append(new StringBuilder()).toString();
}
@Override
public StringBuilder append(StringBuilder sb) {
sb.append("{value=");
sb.append(value);
sb.append('}');
return sb;
}
public StringBuilder append(StringBuilder sb)
Warning: whenever toString() is overridden, you must override append() as well. Not overriding append() will result in incorrect serialization results. The append() method should contain the actual concatenation logic and toString() should simply utilize append(). An example of overriding both methods is as follows:
@Override
public String toString() {
return append(new StringBuilder()).toString();
}
@Override
public StringBuilder append(StringBuilder sb) {
sb.append("{value=");
sb.append(value);
sb.append('}');
return sb;
}
public String toString(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).Copyright © 2022. All rights reserved.