JSONArray
open class JSONArray
| kotlin.Any | |
| ↳ | org.json.JSONArray |
A dense indexed sequence of values. Values may be any mix of JSONObjects, other JSONArrays, Strings, Booleans, Integers, Longs, Doubles, null or JSONObject.NULL. Values may not be NaNs, infinities, or of any type not listed here.
JSONArray has the same type coercion behavior and optional/mandatory accessors as JSONObject. See that class' documentation for details.
Warning: this class represents null in two incompatible ways: the standard Java null reference, and the sentinel value JSONObject.NULL. In particular, get fails if the requested index holds the null reference, but succeeds if it holds JSONObject.NULL.
Instances of this class are not thread safe. Although this class is nonfinal, it was not designed for inheritance and should not be subclassed. In particular, self-use by overridable methods is not specified. See Effective Java Item 17, "Design and Document or inheritance or else prohibit it" for further information.
Summary
| Public constructors | |
|---|---|
|
Creates a |
|
|
Creates a new |
|
|
Creates a new |
|
JSONArray(copyFrom: MutableCollection<Any?>!)Creates a new |
|
JSONArray(readFrom: JSONTokener!)Creates a new |
|
| Public methods | |
|---|---|
| open Boolean |
Indicates whether some other object is "equal to" this one. |
| open Any! |
Returns the value at |
| open Boolean |
getBoolean(index: Int)Returns the value at |
| open Double |
Returns the value at |
| open Int |
Returns the value at |
| open JSONArray! |
getJSONArray(index: Int)Returns the value at |
| open JSONObject! |
getJSONObject(index: Int)Returns the value at |
| open Long |
Returns the value at |
| open String! |
Returns the value at |
| open Int |
hashCode()Returns a hash code value for the object. |
| open Boolean |
Returns true if this array has no value at |
| open String! |
Returns a new string by alternating this array's values with |
| open Int |
length()Returns the number of values in this array. |
| open Any! |
Returns the value at |
| open Boolean |
optBoolean(index: Int)Returns the value at |
| open Boolean |
optBoolean(index: Int, fallback: Boolean)Returns the value at |
| open Double |
Returns the value at |
| open Double |
Returns the value at |
| open Int |
Returns the value at |
| open Int |
Returns the value at |
| open JSONArray! |
optJSONArray(index: Int)Returns the value at |
| open JSONObject! |
optJSONObject(index: Int)Returns the value at |
| open Long |
Returns the value at |
| open Long |
Returns the value at |
| open String! |
Returns the value at |
| open String! |
Returns the value at |
| open JSONArray! |
Appends |
| open JSONArray! |
Appends |
| open JSONArray! |
Appends |
| open JSONArray! |
Sets the value at |
| open JSONArray! |
Sets the value at |
| open JSONArray! |
Sets the value at |
| open JSONArray! |
Sets the value at |
| open JSONArray! |
Sets the value at |
| open JSONArray! |
Appends |
| open JSONArray! |
Appends |
| open Any! |
Removes and returns the value at |
| open JSONObject! |
toJSONObject(names: JSONArray!)Returns a new object whose values are the values in this array, and whose names are the values in |
| open String |
toString()Encodes this array as a compact JSON string, such as: [94043,90210] |
| open String! |
Encodes this array as a human readable JSON string for debugging, such as: [ 94043, 90210 ] |
Public constructors
JSONArray
JSONArray(array: Any!)
Creates a new JSONArray with values from the given primitive array.
JSONArray
JSONArray(json: String!)
Creates a new JSONArray with values from the JSON string.
| Parameters | |
|---|---|
json |
String!: a JSON-encoded string containing an array. |
| Exceptions | |
|---|---|
org.json.JSONException |
if the parse fails or doesn't yield a JSONArray. |
JSONArray
JSONArray(copyFrom: MutableCollection<Any?>!)
Creates a new JSONArray by copying all values from the given collection.
| Parameters | |
|---|---|
copyFrom |
MutableCollection<Any?>!: a collection whose values are of supported types. Unsupported values are not permitted and will yield an array in an inconsistent state. |
JSONArray
JSONArray(readFrom: JSONTokener!)
Creates a new JSONArray with values from the next array in the tokener.
| Parameters | |
|---|---|
readFrom |
JSONTokener!: a tokener whose nextValue() method will yield a JSONArray. |
| Exceptions | |
|---|---|
org.json.JSONException |
if the parse fails or doesn't yield a JSONArray. |
Public methods
equals
open fun equals(other: Any?): Boolean
Indicates whether some other object is "equal to" this one.
The equals method implements an equivalence relation on non-null object references:
- It is reflexive: for any non-null reference value
x,x.equals(x)should returntrue. - It is symmetric: for any non-null reference values
xandy,x.equals(y)should returntrueif and only ify.equals(x)returnstrue. - It is transitive: for any non-null reference values
x,y, andz, ifx.equals(y)returnstrueandy.equals(z)returnstrue, thenx.equals(z)should returntrue. - It is consistent: for any non-null reference values
xandy, multiple invocations ofx.equals(y)consistently returntrueor consistently returnfalse, provided no information used inequalscomparisons on the objects is modified. - For any non-null reference value
x,x.equals(null)should returnfalse.
An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.
| Parameters | |
|---|---|
obj |
the reference object with which to compare. |
| Return | |
|---|---|
Boolean |
true if this object is the same as the obj argument; false otherwise. |
get
open fun get(index: Int): Any!
Returns the value at index.
| Exceptions | |
|---|---|
org.json.JSONException |
if this array has no value at index, or if that value is the null reference. This method returns normally if the value is JSONObject#NULL. |
getBoolean
open fun getBoolean(index: Int): Boolean
Returns the value at index if it exists and is a boolean or can be coerced to a boolean.
| Exceptions | |
|---|---|
org.json.JSONException |
if the value at index doesn't exist or cannot be coerced to a boolean. |
getDouble
open fun getDouble(index: Int): Double
Returns the value at index if it exists and is a double or can be coerced to a double.
| Exceptions | |
|---|---|
org.json.JSONException |
if the value at index doesn't exist or cannot be coerced to a double. |
getInt
open fun getInt(index: Int): Int
Returns the value at index if it exists and is an int or can be coerced to an int.
| Exceptions | |
|---|---|
org.json.JSONException |
if the value at index doesn't exist or cannot be coerced to a int. |
getJSONArray
open fun getJSONArray(index: Int): JSONArray!
Returns the value at index if it exists and is a JSONArray.
| Exceptions | |
|---|---|
org.json.JSONException |
if the value doesn't exist or is not a JSONArray. |
getJSONObject
open fun getJSONObject(index: Int): JSONObject!
Returns the value at index if it exists and is a JSONObject.
| Exceptions | |
|---|---|
org.json.JSONException |
if the value doesn't exist or is not a JSONObject. |
getLong
open fun getLong(index: Int): Long
Returns the value at index if it exists and is a long or can be coerced to a long.
| Exceptions | |
|---|---|
org.json.JSONException |
if the value at index doesn't exist or cannot be coerced to a long. |
getString
open fun getString(index: Int): String!
Returns the value at index if it exists, coercing it if necessary.
| Exceptions | |
|---|---|
org.json.JSONException |
if no such value exists. |
hashCode
open fun hashCode(): Int
Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.
The general contract of hashCode is:
- Whenever it is invoked on the same object more than once during an execution of a Java application, the
hashCodemethod must consistently return the same integer, provided no information used inequalscomparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application. - If two objects are equal according to the
equalsmethod, then calling thehashCodemethod on each of the two objects must produce the same integer result. - It is not required that if two objects are unequal according to the
equalsmethod, then calling thehashCodemethod on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
| Return | |
|---|---|
Int |
a hash code value for this object. |
isNull
open fun isNull(index: Int): Boolean
Returns true if this array has no value at index, or if its value is the null reference or JSONObject.NULL.
join
open fun join(separator: String!): String!
Returns a new string by alternating this array's values with separator. This array's string values are quoted and have their special characters escaped. For example, the array containing the strings '12" pizza', 'taco' and 'soda' joined on '+' returns this:
"12\" pizza"+"taco"+"soda"
opt
open fun opt(index: Int): Any!
Returns the value at index, or null if the array has no value at index.
optBoolean
open fun optBoolean(index: Int): Boolean
Returns the value at index if it exists and is a boolean or can be coerced to a boolean. Returns false otherwise.
optBoolean
open fun optBoolean(
index: Int,
fallback: Boolean
): Boolean
Returns the value at index if it exists and is a boolean or can be coerced to a boolean. Returns fallback otherwise.
optDouble
open fun optDouble(index: Int): Double
Returns the value at index if it exists and is a double or can be coerced to a double. Returns NaN otherwise.
optDouble
open fun optDouble(
index: Int,
fallback: Double
): Double
Returns the value at index if it exists and is a double or can be coerced to a double. Returns fallback otherwise.
optInt
open fun optInt(index: Int): Int
Returns the value at index if it exists and is an int or can be coerced to an int. Returns 0 otherwise.
optInt
open fun optInt(
index: Int,
fallback: Int
): Int
Returns the value at index if it exists and is an int or can be coerced to an int. Returns fallback otherwise.
optJSONArray
open fun optJSONArray(index: Int): JSONArray!
Returns the value at index if it exists and is a JSONArray. Returns null otherwise.
optJSONObject
open fun optJSONObject(index: Int): JSONObject!
Returns the value at index if it exists and is a JSONObject. Returns null otherwise.
optLong
open fun optLong(index: Int): Long
Returns the value at index if it exists and is a long or can be coerced to a long. Returns 0 otherwise.
optLong
open fun optLong(
index: Int,
fallback: Long
): Long
Returns the value at index if it exists and is a long or can be coerced to a long. Returns fallback otherwise.
optString
open fun optString(index: Int): String!
Returns the value at index if it exists, coercing it if necessary. Returns the empty string if no such value exists.
optString
open fun optString(
index: Int,
fallback: String!
): String!
Returns the value at index if it exists, coercing it if necessary. Returns fallback if no such value exists.
put
open fun put(value: Boolean): JSONArray!
Appends value to the end of this array.
| Return | |
|---|---|
JSONArray! |
this array. |
put
open fun put(value: Double): JSONArray!
Appends value to the end of this array.
| Parameters | |
|---|---|
value |
Double: a finite value. May not be NaNs or infinities. |
| Return | |
|---|---|
JSONArray! |
this array. |
put
open fun put(value: Int): JSONArray!
Appends value to the end of this array.
| Return | |
|---|---|
JSONArray! |
this array. |
put
open fun put(
index: Int,
value: Boolean
): JSONArray!
Sets the value at index to value, null padding this array to the required length if necessary. If a value already exists at index, it will be replaced.
| Return | |
|---|---|
JSONArray! |
this array. |
put
open fun put(
index: Int,
value: Double
): JSONArray!
Sets the value at index to value, null padding this array to the required length if necessary. If a value already exists at index, it will be replaced.
| Parameters | |
|---|---|
value |
Double: a finite value. May not be NaNs or infinities. |
| Return | |
|---|---|
JSONArray! |
this array. |
put
open fun put(
index: Int,
value: Int
): JSONArray!
Sets the value at index to value, null padding this array to the required length if necessary. If a value already exists at index, it will be replaced.
| Return | |
|---|---|
JSONArray! |
this array. |
put
open fun put(
index: Int,
value: Any!
): JSONArray!
Sets the value at index to value, null padding this array to the required length if necessary. If a value already exists at index, it will be replaced.
| Parameters | |
|---|---|
value |
Any!: a JSONObject, JSONArray, String, Boolean, Integer, Long, Double, JSONObject.NULL, or null. May not be NaNs or infinities. |
| Return | |
|---|---|
JSONArray! |
this array. |
put
open fun put(
index: Int,
value: Long
): JSONArray!
Sets the value at index to value, null padding this array to the required length if necessary. If a value already exists at index, it will be replaced.
| Return | |
|---|---|
JSONArray! |
this array. |
put
open fun put(value: Any!): JSONArray!
Appends value to the end of this array.
| Parameters | |
|---|---|
value |
Any!: a JSONObject, JSONArray, String, Boolean, Integer, Long, Double, JSONObject.NULL, or null. May not be NaNs or infinities. Unsupported values are not permitted and will cause the array to be in an inconsistent state. |
| Return | |
|---|---|
JSONArray! |
this array. |
put
open fun put(value: Long): JSONArray!
Appends value to the end of this array.
| Return | |
|---|---|
JSONArray! |
this array. |
remove
open fun remove(index: Int): Any!
Removes and returns the value at index, or null if the array has no value at index.
toJSONObject
open fun toJSONObject(names: JSONArray!): JSONObject!
Returns a new object whose values are the values in this array, and whose names are the values in names. Names and values are paired up by index from 0 through to the shorter array's length. Names that are not strings will be coerced to strings. This method returns null if either array is empty.
toString
open fun toString(): String
Encodes this array as a compact JSON string, such as:
[94043,90210]
| Return | |
|---|---|
String |
a string representation of the object. |
toString
open fun toString(indentSpaces: Int): String!
Encodes this array as a human readable JSON string for debugging, such as:
[ 94043, 90210 ]
| Parameters | |
|---|---|
indentSpaces |
Int: the number of spaces to indent for each level of nesting. |