public class JSONObject extends JSONGetter<String> implements JSON, Map<String,Object>
myString = new JSONObject().put("JSON", "Hello, World!").toString();
| 构造器和说明 |
|---|
JSONObject()
空构造
|
JSONObject(JSONObject jsonObject,
String... names)
使用其他
JSONObject构造新的JSONObject,并只加入指定name对应的键值对。 |
JSONObject(JSONTokener x)
Construct a JSONObject from a JSONTokener.
|
JSONObject(Object source)
构建JSONObject,如果给定值为Map,将键值对加入JSON对象;
如果为普通的JavaBean,调用其getters方法(getXXX或者isXXX)获得值,加入到JSON对象 例如:如果JavaBean对象中有个方法getName(),值为"张三",获得的键值对为:name: "张三" |
JSONObject(Object object,
String[] names)
Construct a JSONObject from an Object, using reflection to find the public members.
|
JSONObject(String source)
从JSON字符串解析为JSON对象
|
| 限定符和类型 | 方法和说明 |
|---|---|
JSONObject |
accumulate(String key,
Object value)
积累值。
|
JSONObject |
append(String key,
Object value)
追加值,如果key无对应值,就添加一个JSONArray,其元素只有value,如果值已经是一个JSONArray,则添加到值JSONArray中。
|
void |
clear() |
boolean |
containsKey(Object key) |
boolean |
containsValue(Object value) |
Set<Map.Entry<String,Object>> |
entrySet() |
boolean |
equals(Object other)
JSON中的key以及对应的value相等则判定为与此对象相同
|
Object |
get(Object key) |
Object |
getObj(String key,
Object defaultValue)
获取Object属性值
|
JSONObject |
increment(String key)
对值加一,如果值不存在,赋值1,如果为数字类型,做加一操作
|
boolean |
isEmpty() |
boolean |
isNull(String key)
Determine if the value associated with the key is null or if there is no value.
|
Set<String> |
keySet() |
JSONObject |
put(String key,
Object value)
PUT 键值对到JSONObject中,如果值为
null,将此键移除 |
void |
putAll(Map<? extends String,? extends Object> m) |
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到JSONObject中
|
Object |
remove(Object key) |
int |
size() |
<T> T |
toBean(Class<T> clazz)
转为实体类对象
|
<T> T |
toBean(T bean)
转为实体类对象
|
JSONArray |
toJSONArray(Collection<String> names)
将指定KEY列表的值组成新的JSONArray
|
String |
toJSONString(int indentFactor)
Make a prettyprinted JSON text of this JSONObject.
|
String |
toString()
Make a JSON text of this JSONObject.
|
Collection<Object> |
values() |
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. |
getJSONArray, getJSONObjectgetBigDecimal, getBigInteger, getBool, getByte, getChar, getDouble, getEnum, getFloat, getInt, getLong, getShort, getStrgetBigDecimal, getBigInteger, getBool, getByte, getChar, getDouble, getEnum, 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(Object source)
source - JavaBean或者Map对象或者Stringpublic 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 - 以大括号 {} 包围的字符串,其中KEY和VALUE使用 : 分隔,每个键值对使用逗号分隔JSONException - JSON字符串语法错误public boolean isNull(String key)
key - A key string.public JSONArray toJSONArray(Collection<String> names) throws JSONException
names - KEY列表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 boolean containsKey(Object key)
containsKey 在接口中 Map<String,Object>public boolean containsValue(Object value)
containsValue 在接口中 Map<String,Object>public Object getObj(String key, Object defaultValue)
OptBasicTypeGettergetObj 在接口中 OptBasicTypeGetter<String>getObj 在类中 OptNullBasicTypeFromObjectGetter<String>key - 属性名defaultValue - 默认值public JSONObject put(String key, Object value) throws JSONException
null,将此键移除put 在接口中 Map<String,Object>key - A key string.value - 值对象. 可以是以下类型: Boolean, Double, Integer, JSONArray, JSONObject, Long, String, or the JSONNull.NULL.JSONException - 值是无穷数字抛出此异常public JSONObject putOnce(String key, Object value) throws JSONException
key - 键value - 值对象,可以是以下类型: Boolean, Double, Integer, JSONArray, JSONObject, Long, String, or the JSONNull.NULL.JSONException - 值是无穷数字、键重复抛出异常public JSONObject putOpt(String key, Object value) throws JSONException
key - 键value - 值对象,可以是以下类型: Boolean, Double, Integer, JSONArray, JSONObject, Long, String, or the JSONNull.NULL.JSONException - 值是无穷数字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 - 如果给定键为null或者键对应的值存在且为非JSONArraypublic 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 equals(Object other)
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 © 2017. All rights reserved.