public class JSONObject extends OptNullBasicTypeFromObjectGetter<String> implements JSON
get 和 opt 方法通过name获得value, 使用 put方法增加或替换值。Boolean, JSONArray, JSONObject, Number, String, 或者 JSONNull.NULL put 方法示例:
myString = new JSONObject().put("JSON", "Hello, World!").toString();
结果: {"JSON": "Hello, World"}. toString 生成的字符串严格遵循JSON语法格式. 构造访问传入的JSON可以兼容更宽泛的语法:
, (逗号) 可以出现在最后一个花括号之前。' (单引号)包围。{ } [ ] / \ : , #也不能是数字或者true, false, or null这些关键字。| 构造器和说明 |
|---|
JSONObject()
空构造
|
JSONObject(JSONObject jsonObject,
String... names)
使用其他
JSONObject构造新的JSONObject,并只加入指定name对应的键值对。 |
JSONObject(JSONTokener x)
Construct a JSONObject from a JSONTokener.
|
JSONObject(Map<?,?> map)
Construct a JSONObject from a Map.
|
JSONObject(Object bean)
Construct a JSONObject from an Object using bean getters.
|
JSONObject(Object object,
String[] names)
Construct a JSONObject from an Object, using reflection to find the public members.
|
JSONObject(String source)
Construct a JSONObject from a source JSON text string.
|
JSONObject(String baseName,
Locale locale)
Construct a JSONObject from a ResourceBundle.
|
| 限定符和类型 | 方法和说明 |
|---|---|
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.
|
Object |
get(String key)
获得指定KEY对应的值
|
<E extends Enum<E>> |
getEnum(Class<E> clazz,
String key)
获得Enum类型的值
|
<E extends Enum<E>> |
getEnum(Class<E> clazz,
String key,
E defaultValue)
获得Enum类型的值
|
JSONArray |
getJSONArray(String key)
获得JSONArray对象
|
JSONObject |
getJSONObject(String key)
获得JSONObject对象
|
Object |
getObj(String key,
Object defaultValue)
获取Object属性值
|
boolean |
has(String key)
是否存在指定KEY
|
JSONObject |
increment(String key)
Increment a property of a JSONObject.
|
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.
|
Set<String> |
keySet()
Get a set of 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.
|
JSONObject |
put(String key,
boolean value)
Put a key/boolean pair in the JSONObject.
|
JSONObject |
put(String key,
Collection<?> value)
Put a key/value pair in the JSONObject, where the value will be a JSONArray which is produced from a Collection.
|
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,
long value)
Put a key/long pair in the JSONObject.
|
JSONObject |
put(String key,
Map<?,?> value)
Put a key/value pair in the JSONObject, where the value will be a JSONObject which is produced from a Map.
|
JSONObject |
put(String key,
Object value)
Put a key/value pair in the JSONObject.
|
JSONObject |
putOnce(String key,
Object value)
Put a key/value pair in the JSONObject, but only if the key and the value are both non-null, and only if there is not already a member with that name.
|
JSONObject |
putOpt(String key,
Object value)
Put a key/value pair in the JSONObject, but only if the key and the value are both non-null.
|
Object |
remove(String key)
Remove a name and its value, if present.
|
boolean |
similar(Object other)
Determine if two JSONObjects are similar.
|
<T> T |
toBean(Class<T> clazz)
转为实体类对象
|
<T> T |
toBean(T bean)
转为实体类对象
|
JSONArray |
toJSONArray(JSONArray names)
Produce a JSONArray containing the values of the members of this JSONObject.
|
String |
toJSONString(int indentFactor)
Make a prettyprinted JSON text of this JSONObject.
|
String |
toString()
Make a JSON text of this JSONObject.
|
Writer |
write(Writer writer)
将JSON内容写入Writer,无缩进
Warning: This method assumes that the data structure is acyclical. |
Writer |
write(Writer writer,
int indentFactor,
int indent)
将JSON内容写入Writer
Warning: This method assumes that the data structure is acyclical. |
getBigDecimal, getBigInteger, getBool, getByte, getChar, getDouble, getFloat, getInt, getLong, getShort, getStrgetBigDecimal, getBigInteger, getBool, getByte, getChar, getDouble, getFloat, getInt, getLong, getObj, getShort, getStrpublic JSONObject()
public JSONObject(JSONObject jsonObject, String... names)
JSONObject构造新的JSONObject,并只加入指定name对应的键值对。jsonObject - A JSONObject.names - 需要的name列表public JSONObject(JSONTokener x) throws JSONException
x - A JSONTokener object containing the source string.JSONException - If there is a syntax error in the source string or a duplicated key.public JSONObject(Map<?,?> map)
map - A map object that can be used to initialize the contents of the JSONObject.public JSONObject(Object bean)
"get" or "is" followed by an uppercase letter, the method is invoked, and a key and the value returned from the getter method are put into the new JSONObject.
The key is formed by removing the "get" or "is" prefix. If the second remaining character is not upper case, then the first character is converted to lower case.
For example, if an object has a method named "getName", and if the result of calling object.getName() is "Larry Fine", then the JSONObject will contain
"name": "Larry Fine".bean - An object that has getter methods that should be used to make a JSONObject.public JSONObject(Object object, String[] names)
object - An object that has fields that should be used to make a JSONObject.names - An array of strings, the names of the fields to be obtained from the object.public JSONObject(String source) throws JSONException
source - A string beginning with { (left brace) and ending with } (right brace).JSONException - If there is a syntax error in the source string or a duplicated key.public JSONObject(String baseName, Locale locale) throws JSONException
baseName - The ResourceBundle base name.locale - The Locale to load the ResourceBundle for.JSONException - If any JSONExceptions are detected.public JSONObject accumulate(String key, Object value) throws JSONException
key - A key string.value - An object to be accumulated under the key.JSONException - If the value is an invalid number or if the key is null.public JSONObject append(String key, Object value) throws JSONException
key - A key string.value - An object to be accumulated under the key.JSONException - If the key is null or if the current value associated with the key is not a JSONArray.public boolean has(String key)
key - A key string.public JSONObject increment(String key) throws JSONException
key - A key string.JSONException - If there is already a property with this name that is not an Integer, Long, Double, or Float.public boolean isNull(String key)
key - A key string.public Iterator<String> keys()
public int length()
public JSONArray names()
public Object getObj(String key, Object defaultValue)
OptBasicTypeGettergetObj 在接口中 OptBasicTypeGetter<String>getObj 在类中 OptNullBasicTypeFromObjectGetter<String>key - 属性名defaultValue - 默认值public <E extends Enum<E>> E getEnum(Class<E> clazz, String key)
clazz - Enum的Classkey - KEYpublic <E extends Enum<E>> E getEnum(Class<E> clazz, String key, E defaultValue)
clazz - Enum的Classkey - KEYdefaultValue - 默认值public JSONArray getJSONArray(String key)
key - KEYpublic JSONObject getJSONObject(String key)
key - KEYpublic JSONObject put(String key, boolean value) throws JSONException
key - A key string.value - A boolean which is the value.JSONException - If the key is null.public JSONObject put(String key, Collection<?> value) throws JSONException
key - A key string.value - A Collection value.JSONExceptionpublic JSONObject put(String key, double value) throws JSONException
key - A key string.value - A double which is the value.JSONException - If the key is null or if the number is invalid.public JSONObject put(String key, int value) throws JSONException
key - A key string.value - An int which is the value.JSONException - If the key is null.public JSONObject put(String key, long value) throws JSONException
key - A key string.value - A long which is the value.JSONException - If the key is null.public JSONObject put(String key, Map<?,?> value) throws JSONException
key - A key string.value - A Map value.JSONExceptionpublic JSONObject put(String key, Object value) throws JSONException
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 JSONNull.NULL object.JSONException - If the value is non-finite number or if the key is null.public JSONObject putOnce(String key, Object value) throws JSONException
key - stringvalue - objectJSONException - if the key is a duplicatepublic JSONObject putOpt(String key, Object value) throws JSONException
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 JSONNull.NULL object.JSONException - If the value is a non-finite number.public Object remove(String key)
key - The name to be removed.public boolean similar(Object other)
other - The other JSONObjectpublic JSONArray toJSONArray(JSONArray names) throws JSONException
names - A JSONArray containing a list of key strings. This determines the sequence of the values in the result.JSONException - If any of the values are non-finite numbers.public <T> T toBean(Class<T> clazz)
clazz - 实体类public <T> T toBean(T bean)
bean - 实体类public String toString()
Warning: This method assumes that the data structure is acyclical.
public String toJSONString(int indentFactor) throws JSONException
Warning: This method assumes that the data structure is acyclical.
toJSONString 在接口中 JSONindentFactor - The number of spaces to add to each level of indentation.{ (left brace) and ending with }
(right brace).JSONException - If the object contains an invalid number.public Writer write(Writer writer) throws JSONException
JSONwrite 在接口中 JSONwriter - WriterJSONExceptionpublic Writer write(Writer writer, int indentFactor, int indent) throws JSONException
JSONwrite 在接口中 JSONwriter - writerindentFactor - 每一级别的缩进量indent - 顶级别缩进量JSONExceptionCopyright © 2016. All rights reserved.