JsonArray

Represents JSON array object.

To parse string into JsonObject use JsonParse.

Instance attributes

size: int

Number of elements in array

values: Array

NXSL array with JSON array elements

Instance methods

The append, insert and set methods accept nested JsonObject and JsonArray values as well as other values converted to JSON. A nested JsonObject or JsonArray is stored by reference, not copied: changing it after it was added also changes this array. Likewise, a nested object or array returned by get is the one stored in this array, so changing it changes this array.

inner = new JsonObject();
outer = new JsonArray();
outer.append(inner);
inner.set("name", "value");
println(outer.get(0).get("name"));  // prints "value"

For the insert* and set* methods, index arguments are interpreted as signed 32-bit integers. A non-integer index causes a runtime error.

append(value): void

Appends value to JSON array.

Table 1. Parameters

value

?

Add value to JSON array. Value type may vary.

get(index) ⇒ ?

Returns array value by the index. Value type depends on the type used in JSON.

Table 2. Parameters

index

Integer

Index of element in array

Return

Element value, or null if index is out of range. Nested JSON objects and arrays are returned as JsonObject and JsonArray.

insert(index, value): bool

Inserts a value at the specified index, shifting subsequent elements to the right.

Table 3. Parameters

index

Integer

Index of element in array

value

?

Add value to JSON array. Value type may vary.

Return

true if the value was inserted; false if the index is negative, greater than size, or the insertion otherwise fails. An index equal to size inserts at the end of the array.

serialize(): String

Returns string with serialized JSON

Return

String with serialized JSON

set(index, value): bool

Replaces the existing value at the specified index.

Table 4. Parameters

index

Integer

Index of element in array

value

?

Add value to JSON array. Value type may vary.

Return

true if the value was replaced; false if the index is negative, greater than or equal to size, or the replacement otherwise fails.

appendBoolean(value): void

Appends a boolean value to the end of the JSON array.

Table 5. Parameters

value

any

Value to convert to boolean

appendNull(): void

Appends a JSON null value to the end of the JSON array.

appendString(value): void

Appends a string value to the end of the JSON array.

Table 6. Parameters

value

any

Value to convert to string

insertBoolean(index, value): bool

Inserts a boolean value at the specified index in the JSON array.

Table 7. Parameters

index

Integer

Index to insert at

value

any

Value to convert to boolean

Return

true if the value was inserted; false if the index is negative, greater than size, or the insertion otherwise fails. An index equal to size inserts at the end of the array.

insertNull(index): bool

Inserts a JSON null value at the specified index in the JSON array.

Table 8. Parameters

index

Integer

Index to insert at

Return

true if the value was inserted; false if the index is negative, greater than size, or the insertion otherwise fails. An index equal to size inserts at the end of the array.

insertString(index, value): bool

Inserts a string value at the specified index in the JSON array.

Table 9. Parameters

index

Integer

Index to insert at

value

any

Value to convert to string

Return

true if the value was inserted; false if the index is negative, greater than size, or the insertion otherwise fails. An index equal to size inserts at the end of the array.

setBoolean(index, value): bool

Replaces the value at the specified index with a boolean value.

Table 10. Parameters

index

Integer

Index of element in array

value

any

Value to convert to boolean

Return

true if the value was replaced; false if the index is negative, greater than or equal to size, or the replacement otherwise fails.

setNull(index): bool

Replaces the value at the specified index with JSON null.

Table 11. Parameters

index

Integer

Index of element in array

Return

true if the value was replaced; false if the index is negative, greater than or equal to size, or the replacement otherwise fails.

setString(index, value): bool

Replaces the value at the specified index with a string value.

Table 12. Parameters

index

Integer

Index of element in array

value

any

Value to convert to string

Return

true if the value was replaced; false if the index is negative, greater than or equal to size, or the replacement otherwise fails.

Constructors

JsonArray()

Creates new JSON array.

Return

JsonArray object

JsonArray(array)

Creates new JSON array object from NXSL array.

Added in version 5.3

Table 13. Parameters

array

array

NXSL array

Return

JsonArray object