public class JSONObject extends java.lang.Object implements java.lang.Iterable<java.util.Map.Entry<java.lang.String,java.lang.Object>>, Copyable
限定符和类型 | 字段和说明 |
---|---|
static JSONObject |
EMPTY |
static java.lang.Object |
NULL |
构造器和说明 |
---|
JSONObject()
Create a new, empty instance
|
JSONObject(java.util.Map map)
Create an instance from a Map.
|
JSONObject(java.lang.String json)
Create an instance from a string of JSON
|
限定符和类型 | 方法和说明 |
---|---|
JSONObject |
clear()
Remove all the entries in this JSON object
|
boolean |
containsKey(java.lang.String key)
Does the JSON object contain the specified key?
|
JSONObject |
copy()
Copy the JSON object
|
static JSONObject |
create() |
static JSONObject |
create(java.util.Map<java.lang.String,java.lang.Object> map) |
java.lang.String |
encode()
Encode this JSON object as a string.
|
java.lang.String |
encodePrettily()
Encode this JSON object a a string, with whitespace to make the object easier to read by a human, or other
sentient organism.
|
boolean |
equals(java.lang.Object o) |
java.util.Set<java.lang.String> |
fieldNames()
Return the set of field names in the JSON objects
|
java.lang.Object |
get(java.lang.String key) |
byte[] |
getBinary(java.lang.String key)
Get the binary value with the specified key.
|
byte[] |
getBinary(java.lang.String key,
byte[] def)
Like
getBinary(String) but specifying a default value to return if there is no entry. |
boolean |
getBoolean(java.lang.String key) |
double |
getDouble(java.lang.String key) |
float |
getFloat(java.lang.String key) |
java.util.Date |
getInstant(java.lang.String key) |
int |
getInt(java.lang.String key) |
JSONArray |
getJsonArray(java.lang.String key)
Get the JsonArray value with the specified key
|
JSONArray |
getJSONArray(java.lang.String key) |
JSONArray |
getJsonArray(java.lang.String key,
JSONArray def)
Like
getJsonArray(String) but specifying a default value to return if there is no entry. |
JSONObject |
getJsonObject(java.lang.String key)
Get the JsonObject value with the specified key
|
JSONObject |
getJSONObject(java.lang.String key) |
JSONObject |
getJsonObject(java.lang.String key,
JSONObject def)
Like
getJsonObject(String) but specifying a default value to return if there is no entry. |
long |
getLong(java.lang.String key) |
java.util.Map<java.lang.String,java.lang.Object> |
getMap()
Get the underlying
Map as is. |
java.lang.Double |
getNumberOfDouble(java.lang.String key)
Get the Double value with the specified key
|
java.lang.Double |
getNumberOfDouble(java.lang.String key,
java.lang.Double def)
Like
getNumberOfDouble(String) but specifying a default value to return if there is no entry. |
java.lang.Float |
getNumberOfFloat(java.lang.String key)
Get the Float value with the specified key
|
java.lang.Float |
getNumberOfFloat(java.lang.String key,
java.lang.Float def)
Like
getNumberOfFloat(String) but specifying a default value to return if there is no entry. |
java.lang.Integer |
getNumberOfInteger(java.lang.String key)
Get the Integer value with the specified key
|
java.lang.Integer |
getNumberOfInteger(java.lang.String key,
java.lang.Integer def)
Like
getNumberOfInteger(String) but specifying a default value to return if there is no entry. |
java.lang.Long |
getNumberOfLong(java.lang.String key)
Get the Long value with the specified key
|
java.lang.Long |
getNumberOfLong(java.lang.String key,
java.lang.Long def)
Like
getNumberOfLong(String) but specifying a default value to return if there is no entry. |
java.lang.Boolean |
getObjectBoolean(java.lang.String key)
Get the Boolean value with the specified key
|
java.lang.Boolean |
getObjectBoolean(java.lang.String key,
java.lang.Boolean def)
Like
getObjectBoolean(String) but specifying a default value to return if there is no entry. |
java.lang.String |
getString(java.lang.String key)
Get the string value with the specified key
|
java.lang.String |
getString(java.lang.String key,
java.lang.String def)
Like
getString(String) but specifying a default value to return if there is no entry. |
java.lang.Object |
getValue(java.lang.String key)
Get the value with the specified key, as an Object
|
java.lang.Object |
getValue(java.lang.String key,
java.lang.Object def)
Like
getValue(String) but specifying a default value to return if there is no entry. |
boolean |
has(java.lang.String key)
判断是否包含键
|
int |
hashCode() |
boolean |
isEmpty()
Is this object entry?
|
java.util.Iterator<java.util.Map.Entry<java.lang.String,java.lang.Object>> |
iterator()
Get an Iterator of the entries in the JSON object.
|
JSONObject |
join(JSONObject target)
已过时。
|
java.util.Iterator<java.lang.String> |
keys() |
int |
length() |
static void |
main(java.lang.String[] args) |
static JSONObject |
mapFrom(java.lang.Object obj)
Create a JsonObject from the fields of a Java object.
|
<T> T |
mapTo(java.lang.Class<T> type)
Instantiate a Java object from a JsonObject.
|
JSONObject |
mergeIn(JSONObject other)
Merge in another JSON object.
|
JSONObject |
mergeIn(JSONObject other,
boolean deep)
Merge in another JSON object.
|
JSONObject |
mergeIn(JSONObject other,
int depth)
Merge in another JSON object.
|
java.lang.Object |
opt(java.lang.String key) |
boolean |
optBoolean(java.lang.String key) |
boolean |
optBoolean(java.lang.String key,
boolean defaultValue) |
double |
optDouble(java.lang.String key) |
double |
optDouble(java.lang.String key,
double defaultValue) |
int |
optInt(java.lang.String key) |
int |
optInt(java.lang.String key,
int defaultValue) |
JSONArray |
optJSONArray(java.lang.String key) |
JSONObject |
optJSONObject(java.lang.String key) |
long |
optLong(java.lang.String key) |
long |
optLong(java.lang.String key,
long defaultValue) |
java.lang.String |
optString(java.lang.String key) |
java.lang.String |
optString(java.lang.String key,
java.lang.String defaultValue) |
JSONObject |
put(java.lang.String key,
boolean value)
Put a boolean into the JSON object with the specified key.
|
JSONObject |
put(java.lang.String key,
java.lang.Boolean value)
Put a Boolean into the JSON object with the specified key.
|
JSONObject |
put(java.lang.String key,
byte[] value)
Put a byte[] into the JSON object with the specified key.
|
JSONObject |
put(java.lang.String key,
java.lang.CharSequence value)
Put an CharSequence into the JSON object with the specified key.
|
JSONObject |
put(java.lang.String key,
java.util.Collection c)
已过时。
|
JSONObject |
put(java.lang.String key,
java.util.Date value) |
JSONObject |
put(java.lang.String key,
double value)
Put a double into the JSON object with the specified key.
|
JSONObject |
put(java.lang.String key,
java.lang.Double value)
Put a Double into the JSON object with the specified key.
|
JSONObject |
put(java.lang.String key,
java.lang.Enum value)
Put an Enum into the JSON object with the specified key.
|
JSONObject |
put(java.lang.String key,
float value)
Put a float into the JSON object with the specified key.
|
JSONObject |
put(java.lang.String key,
java.lang.Float value)
Put a Float into the JSON object with the specified key.
|
JSONObject |
put(java.lang.String key,
int value)
Put an int into the JSON object with the specified key.
|
JSONObject |
put(java.lang.String key,
java.lang.Integer value)
Put an Integer into the JSON object with the specified key.
|
JSONObject |
put(java.lang.String key,
JSONArray value)
Put a JSON array into the JSON object with the specified key.
|
JSONObject |
put(java.lang.String key,
JSONObject value)
Put another JSON object into the JSON object with the specified key.
|
JSONObject |
put(java.lang.String key,
long value)
Put a long into the JSON object with the specified key.
|
JSONObject |
put(java.lang.String key,
java.lang.Long value)
Put a Long into the JSON object with the specified key.
|
JSONObject |
put(java.lang.String key,
java.util.Map<java.lang.String,java.lang.Object> value) |
JSONObject |
put(java.lang.String key,
java.lang.Object value)
Put an Object into the JSON object with the specified key.
|
JSONObject |
put(java.lang.String key,
java.lang.String value)
Put a String into the JSON object with the specified key.
|
JSONObject |
putNull(java.lang.String key)
Put a null value into the JSON object with the specified key.
|
JSONObject |
putOpt(java.lang.String key,
java.lang.Object value) |
java.lang.Object |
remove(java.lang.String key)
Remove an entry from this object.
|
int |
size()
Get the number of entries in the JSON object
|
java.util.Map<java.lang.String,java.lang.Object> |
toMap() |
java.lang.String |
toString() |
public static final java.lang.Object NULL
public static final JSONObject EMPTY
public JSONObject(java.lang.String json)
json
- the string of JSONpublic JSONObject()
public JSONObject(java.util.Map map)
map
- the map to create the instance from.public static JSONObject create()
public static JSONObject create(java.util.Map<java.lang.String,java.lang.Object> map)
public static JSONObject mapFrom(java.lang.Object obj)
obj is null
.
obj
- The object to convert to a JsonObject.java.lang.IllegalArgumentException
- if conversion fails due to an incompatible type.public <T> T mapTo(java.lang.Class<T> type)
type
- The type to instantiate from the JsonObject.java.lang.IllegalArgumentException
- if the type cannot be instantiated.public boolean has(java.lang.String key)
key
- 键public java.lang.String getString(java.lang.String key)
key
- the key to return the value forpublic java.lang.Integer getNumberOfInteger(java.lang.String key)
key
- the key to return the value forpublic java.lang.Long getNumberOfLong(java.lang.String key)
key
- the key to return the value forpublic java.lang.Double getNumberOfDouble(java.lang.String key)
key
- the key to return the value forpublic java.lang.Float getNumberOfFloat(java.lang.String key)
key
- the key to return the value forpublic java.lang.Boolean getObjectBoolean(java.lang.String key)
key
- the key to return the value forpublic JSONObject getJsonObject(java.lang.String key)
key
- the key to return the value forjava.lang.ClassCastException
- if the value is not a JsonObjectpublic JSONArray getJsonArray(java.lang.String key)
key
- the key to return the value forjava.lang.ClassCastException
- if the value is not a JsonArraypublic byte[] getBinary(java.lang.String key)
JSON itself has no notion of a binary, this extension complies to the RFC-7493, so this method assumes there is a String value with the key and it contains a Base64 encoded binary, which it decodes if found and returns.
This method should be used in conjunction with put(String, byte[])
key
- the key to return the value forjava.lang.ClassCastException
- if the value is not a Stringjava.lang.IllegalArgumentException
- if the String value is not a legal Base64 encoded valuepublic java.util.Date getInstant(java.lang.String key)
public java.lang.Object getValue(java.lang.String key)
key
- the key to lookuppublic java.lang.String getString(java.lang.String key, java.lang.String def)
getString(String)
but specifying a default value to return if there is no entry.key
- the key to lookupdef
- the default value to use if the entry is not presentdef
if no entry presentpublic java.lang.Integer getNumberOfInteger(java.lang.String key, java.lang.Integer def)
getNumberOfInteger(String)
but specifying a default value to return if there is no entry.key
- the key to lookupdef
- the default value to use if the entry is not presentdef
if no entry presentpublic java.lang.Long getNumberOfLong(java.lang.String key, java.lang.Long def)
getNumberOfLong(String)
but specifying a default value to return if there is no entry.key
- the key to lookupdef
- the default value to use if the entry is not presentdef
if no entry presentpublic java.lang.Double getNumberOfDouble(java.lang.String key, java.lang.Double def)
getNumberOfDouble(String)
but specifying a default value to return if there is no entry.key
- the key to lookupdef
- the default value to use if the entry is not presentdef
if no entry presentpublic java.lang.Float getNumberOfFloat(java.lang.String key, java.lang.Float def)
getNumberOfFloat(String)
but specifying a default value to return if there is no entry.key
- the key to lookupdef
- the default value to use if the entry is not presentdef
if no entry presentpublic java.lang.Boolean getObjectBoolean(java.lang.String key, java.lang.Boolean def)
getObjectBoolean(String)
but specifying a default value to return if there is no entry.key
- the key to lookupdef
- the default value to use if the entry is not presentdef
if no entry presentpublic JSONObject getJsonObject(java.lang.String key, JSONObject def)
getJsonObject(String)
but specifying a default value to return if there is no entry.key
- the key to lookupdef
- the default value to use if the entry is not presentdef
if no entry presentpublic JSONArray getJsonArray(java.lang.String key, JSONArray def)
getJsonArray(String)
but specifying a default value to return if there is no entry.key
- the key to lookupdef
- the default value to use if the entry is not presentdef
if no entry presentpublic byte[] getBinary(java.lang.String key, byte[] def)
getBinary(String)
but specifying a default value to return if there is no entry.key
- the key to lookupdef
- the default value to use if the entry is not presentdef
if no entry presentpublic java.lang.Object getValue(java.lang.String key, java.lang.Object def)
getValue(String)
but specifying a default value to return if there is no entry.key
- the key to lookupdef
- the default value to use if the entry is not presentdef
if no entry presentpublic boolean containsKey(java.lang.String key)
key
- the keypublic java.util.Set<java.lang.String> fieldNames()
public JSONObject put(java.lang.String key, java.lang.Enum value)
JSON has no concept of encoding Enums, so the Enum will be converted to a String using the Enum.name()
method and the value put as a String.
key
- the keyvalue
- the valuepublic JSONObject put(java.lang.String key, java.lang.CharSequence value)
key
- the keyvalue
- the valuepublic JSONObject put(java.lang.String key, java.lang.String value)
key
- the keyvalue
- the valuepublic JSONObject put(java.lang.String key, java.lang.Integer value)
key
- the keyvalue
- the valuepublic JSONObject put(java.lang.String key, int value)
key
- the keyvalue
- the valuepublic JSONObject put(java.lang.String key, java.lang.Long value)
key
- the keyvalue
- the valuepublic JSONObject put(java.lang.String key, long value)
key
- the keyvalue
- the valuepublic JSONObject put(java.lang.String key, java.lang.Double value)
key
- the keyvalue
- the valuepublic JSONObject put(java.lang.String key, double value)
key
- the keyvalue
- the valuepublic JSONObject put(java.lang.String key, java.lang.Float value)
key
- the keyvalue
- the valuepublic JSONObject put(java.lang.String key, float value)
key
- the keyvalue
- the valuepublic JSONObject put(java.lang.String key, java.lang.Boolean value)
key
- the keyvalue
- the valuepublic JSONObject put(java.lang.String key, boolean value)
key
- the keyvalue
- the valuepublic JSONObject putNull(java.lang.String key)
key
- the keypublic JSONObject put(java.lang.String key, java.util.Map<java.lang.String,java.lang.Object> value)
public JSONObject put(java.lang.String key, JSONObject value)
key
- the keyvalue
- the valuepublic JSONObject put(java.lang.String key, JSONArray value)
key
- the keyvalue
- the valuepublic JSONObject put(java.lang.String key, byte[] value)
JSON extension RFC7493, binary will first be Base64 encoded before being put as a String.
key
- the keyvalue
- the valuepublic JSONObject put(java.lang.String key, java.util.Date value)
public JSONObject put(java.lang.String key, java.lang.Object value)
key
- the keyvalue
- the valuepublic java.lang.Object remove(java.lang.String key)
key
- the keypublic JSONObject mergeIn(JSONObject other)
This is the equivalent of putting all the entries of the other JSON object into this object. This is not a deep merge, entries containing (sub) JSON objects will be replaced entirely.
other
- the other JSON objectpublic JSONObject mergeIn(JSONObject other, boolean deep)
other
- the other JSON objectdeep
- if true, a deep merge is performedpublic JSONObject mergeIn(JSONObject other, int depth)
other
- the other JSON objectdepth
- depth of mergepublic java.lang.String encode()
public java.lang.String encodePrettily()
public JSONObject copy()
public java.util.Map<java.lang.String,java.lang.Object> getMap()
Map
as is.
This map may contain values that are not the types returned by the JsonObject
.
public java.util.Iterator<java.util.Map.Entry<java.lang.String,java.lang.Object>> iterator()
iterator
在接口中 java.lang.Iterable<java.util.Map.Entry<java.lang.String,java.lang.Object>>
public int size()
public JSONObject clear()
public boolean isEmpty()
public java.lang.String toString()
toString
在类中 java.lang.Object
public boolean equals(java.lang.Object o)
equals
在类中 java.lang.Object
public int hashCode()
hashCode
在类中 java.lang.Object
public int length()
public java.util.Iterator<java.lang.String> keys()
public java.util.Map<java.lang.String,java.lang.Object> toMap()
public JSONObject join(JSONObject target)
public JSONObject put(java.lang.String key, java.util.Collection c)
public JSONObject putOpt(java.lang.String key, java.lang.Object value) throws JSONException
JSONException
public java.lang.Object get(java.lang.String key) throws JSONException
JSONException
public boolean getBoolean(java.lang.String key) throws JSONException
JSONException
public int getInt(java.lang.String key) throws JSONException
JSONException
public long getLong(java.lang.String key) throws JSONException
JSONException
public float getFloat(java.lang.String key) throws JSONException
JSONException
public double getDouble(java.lang.String key) throws JSONException
JSONException
public JSONArray getJSONArray(java.lang.String key) throws JSONException
JSONException
public JSONObject getJSONObject(java.lang.String key) throws JSONException
JSONException
public java.lang.Object opt(java.lang.String key)
public java.lang.String optString(java.lang.String key)
public java.lang.String optString(java.lang.String key, java.lang.String defaultValue)
public int optInt(java.lang.String key)
public int optInt(java.lang.String key, int defaultValue)
public long optLong(java.lang.String key)
public long optLong(java.lang.String key, long defaultValue)
public double optDouble(java.lang.String key)
public double optDouble(java.lang.String key, double defaultValue)
public boolean optBoolean(java.lang.String key)
public boolean optBoolean(java.lang.String key, boolean defaultValue)
public JSONArray optJSONArray(java.lang.String key)
public JSONObject optJSONObject(java.lang.String key)
public static void main(java.lang.String[] args) throws java.lang.Exception
java.lang.Exception