public class JSONArray extends OptNullBasicTypeFromObjectGetter<Integer> implements JSON, Iterable<Object>
get
and opt methods for accessing the values by index, and put methods for adding or replacing values. The values can be any of these types: Boolean,
JSONArray, JSONObject, Number, String, or the JSONNull.NULL object.
The constructor can convert a JSON text into a Java object. The toString method converts to JSON text.
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 coercion for you.
The texts produced by the toString methods strictly conform to JSON syntax rules. The constructors are more forgiving in the texts they will accept:
, (comma) may appear just before the closing bracket.null value will be inserted when there is , (comma) elision.' (single quote).{ } [ ] / \ : , # and if they do not look like numbers and if they are not the reserved words true, false, or null.| 构造器和说明 |
|---|
JSONArray()
Construct an empty JSONArray.
|
JSONArray(JSONTokener x)
Construct a JSONArray from a JSONTokener.
|
JSONArray(Object arrayOrCollection)
Construct a JSONArray from an array or Collection
|
JSONArray(String source)
Construct a JSONArray from a source JSON text.
|
| 限定符和类型 | 方法和说明 |
|---|---|
Object |
get(int index)
Get the optional object value associated with an index.
|
<E extends Enum<E>> |
getEnum(Class<E> clazz,
int index)
Get the enum value associated with a key.
|
<E extends Enum<E>> |
getEnum(Class<E> clazz,
int index,
E defaultValue)
Get the enum value associated with a key.
|
JSONArray |
getJSONArray(int index)
Get the optional JSONArray associated with an index.
|
JSONObject |
getJSONObject(int index)
Get the optional JSONObject associated with an index.
|
Object |
getObj(Integer index,
Object defaultValue)
获取Object属性值
|
boolean |
isNull(int index)
Determine if the value is null.
|
Iterator<Object> |
iterator() |
String |
join(String separator)
Make a string from the contents of this JSONArray.
|
int |
length()
Get the number of elements in the JSONArray, included nulls.
|
JSONArray |
put(boolean value)
Append a boolean value.
|
JSONArray |
put(Collection<?> value)
Put a value in the JSONArray, where the value will be a JSONArray which is produced from a Collection.
|
JSONArray |
put(double value)
Append a double value.
|
JSONArray |
put(int value)
Append an int value.
|
JSONArray |
put(int index,
boolean value)
Put or replace a boolean value in the JSONArray.
|
JSONArray |
put(int index,
Collection<?> value)
Put a value in the JSONArray, where the value will be a JSONArray which is produced from a Collection.
|
JSONArray |
put(int index,
double value)
Put or replace a double value.
|
JSONArray |
put(int index,
int value)
Put or replace an int value.
|
JSONArray |
put(int index,
long value)
Put or replace a long value.
|
JSONArray |
put(int index,
Map<?,?> value)
Put a value in the JSONArray, where the value will be a JSONObject that is produced from a Map.
|
JSONArray |
put(int index,
Object value)
Put or replace an object value in the JSONArray.
|
JSONArray |
put(long value)
Append an long value.
|
JSONArray |
put(Map<?,?> value)
Put a value in the JSONArray, where the value will be a JSONObject which is produced from a Map.
|
JSONArray |
put(Object value)
Append an object value.
|
Object |
remove(int index)
Remove an index and close the hole.
|
boolean |
similar(Object other)
Determine if two JSONArrays are similar.
|
JSONObject |
toJSONObject(JSONArray names)
Produce a JSONObject by combining a JSONArray of names with the values of this JSONArray.
|
String |
toJSONString(int indentFactor)
Make a prettyprinted JSON text of this JSONArray.
|
String |
toString()
Make a JSON text of this JSONArray.
|
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 JSONArray()
public JSONArray(JSONTokener x) throws JSONException
x - A JSONTokenerJSONException - If there is a syntax error.public JSONArray(String source) throws JSONException
source - A string that begins with [ (left bracket) and ends with ] (right bracket).JSONException - If there is a syntax error.public JSONArray(Object arrayOrCollection) throws JSONException
JSONException - If not an array.public boolean isNull(int index)
index - The index must be between 0 and length() - 1.public String join(String separator) throws JSONException
separator string is inserted between each element. Warning: This method assumes that the data structure is acyclical.separator - A string that will be inserted between the elements.JSONException - If the array contains an invalid number.public int length()
public Object get(int index)
index - The index must be between 0 and length() - 1.public Object getObj(Integer index, Object defaultValue)
OptBasicTypeGettergetObj 在接口中 OptBasicTypeGetter<Integer>getObj 在类中 OptNullBasicTypeFromObjectGetter<Integer>index - 属性名defaultValue - 默认值public <E extends Enum<E>> E getEnum(Class<E> clazz, int index)
clazz - The type of enum to retrieve.index - The index must be between 0 and length() - 1.public <E extends Enum<E>> E getEnum(Class<E> clazz, int index, E defaultValue)
clazz - The type of enum to retrieve.index - The index must be between 0 and length() - 1.defaultValue - The default in case the value is not foundpublic JSONArray getJSONArray(int index)
index - subscriptpublic JSONObject getJSONObject(int index)
index - The index must be between 0 and length() - 1.public JSONArray put(boolean value)
value - A boolean value.public JSONArray put(Collection<?> value)
value - A Collection value.public JSONArray put(double value) throws JSONException
value - A double value.JSONException - if the value is not finite.public JSONArray put(int value)
value - An int value.public JSONArray put(long value)
value - A long value.public JSONArray put(Map<?,?> value)
value - A Map value.public JSONArray put(Object value)
value - An object value. The value should be a Boolean, Double, Integer, JSONArray, JSONObject, Long, or String, or the JSONNull.NULL object.public JSONArray put(int index, boolean value) throws JSONException
index - The subscript.value - A boolean value.JSONException - If the index is negative.public JSONArray put(int index, Collection<?> value) throws JSONException
index - The subscript.value - A Collection value.JSONException - If the index is negative or if the value is not finite.public JSONArray put(int index, double value) throws JSONException
index - The subscript.value - A double value.JSONException - If the index is negative or if the value is not finite.public JSONArray put(int index, int value) throws JSONException
index - The subscript.value - An int value.JSONException - If the index is negative.public JSONArray put(int index, long value) throws JSONException
index - The subscript.value - A long value.JSONException - If the index is negative.public JSONArray put(int index, Map<?,?> value) throws JSONException
index - The subscript.value - The Map value.JSONException - If the index is negative or if the the value is an invalid number.public JSONArray put(int index, Object value) throws JSONException
index - The subscript.value - The value to put into the array. The value should be a Boolean, Double, Integer, JSONArray, JSONObject, Long, or String, or the JSONNull.NULL object.JSONException - If the index is negative or if the the value is an invalid number.public Object remove(int index)
index - The index of the element to be removed.public boolean similar(Object other)
other - The other JSONArraypublic JSONObject toJSONObject(JSONArray names) throws JSONException
names - A JSONArray containing a list of key strings. These will be paired with the values.JSONException - If any of the names are null.public String toString()
Warning: This method assumes that the data structure is acyclical.
public String toJSONString(int indentFactor) throws JSONException
toJSONString 在接口中 JSONindentFactor - The number of spaces to add to each level of indentation.[ (left bracket) and ending with ] (right
bracket).JSONExceptionpublic 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.