Skip to content

io microsphere json JSONArray

github-actions[bot] edited this page Mar 21, 2026 · 1 revision

JSONArray

Type: Class | Module: microsphere-java-core | Package: io.microsphere.json

Source: microsphere-java-core/src/main/java/io/microsphere/json/JSONArray.java

Overview

A dense indexed sequence of values. Values may be any mix of JSONObject JSONObjects, other JSONArray JSONArrays, Strings, Booleans, Integers, Longs, Doubles, null or JSONObject#NULL. Values may not be Double#isNaN() NaNs, Double#isInfinite() 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.

Example Usage

`// Create an empty JSONArray
JSONArray array = new JSONArray();

// Add elements to the array
array.put("Hello");
array.put(123);
array.put(true);

// Convert to JSON string
String json = array.toString();  // Result: ["Hello",123,true]

// Parse from JSON string
JSONArray parsedArray = new JSONArray(json);
`

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 for inheritance or else prohibit it" for further information.

Declaration

public class JSONArray

Version Information

  • Introduced in: 0.1.10-SNAPSHOT (current)
  • Current Project Version: 0.1.10-SNAPSHOT

Version Compatibility

This component is tested and compatible with the following Java versions:

Java Version Status
Java 8 ✅ Compatible
Java 11 ✅ Compatible
Java 17 ✅ Compatible
Java 21 ✅ Compatible
Java 25 ✅ Compatible

Examples

// Create an empty JSONArray
JSONArray array = new JSONArray();

// Add elements to the array
array.put("Hello");
array.put(123);
array.put(true);

// Convert to JSON string
String json = array.toString();  // Result: ["Hello",123,true]

// Parse from JSON string
JSONArray parsedArray = new JSONArray(json);

Usage

Maven Dependency

Add the following dependency to your pom.xml:

<dependency>
    <groupId>io.github.microsphere-projects</groupId>
    <artifactId>microsphere-java-core</artifactId>
    <version>${microsphere-java.version}</version>
</dependency>

Tip: Use the BOM (microsphere-java-dependencies) for consistent version management. See the Getting Started guide.

Import

import io.microsphere.json.JSONArray;

API Reference

Public Methods

Method Description
length Creates a JSONArray with no values.
put Appends value to the end of this array.
put Appends value to the end of this array.
put Appends value to the end of this array.
put Appends value to the end of this array.
put Appends value to the end of this array.
put Sets the value at index to value, null padding this array to the
put Sets the value at index to value, null padding this array to the
put Sets the value at index to value, null padding this array to the
put Sets the value at index to value, null padding this array to the
put Sets the value at index to value, null padding this array to the
isNull Returns true if this array has no value at index, or if its value is the
get Returns the value at index.
opt Returns the value at index, or null if the array has no value at
remove Removes and returns the value at index, or null if the array has no value
getBoolean Returns the value at index if it exists and is a boolean or can be coerced
optBoolean Returns the value at index if it exists and is a boolean or can be coerced
optBoolean Returns the value at index if it exists and is a boolean or can be coerced
getDouble Returns the value at index if it exists and is a double or can be coerced
optDouble Returns the value at index if it exists and is a double or can be coerced

Method Details

length

public int length()

Creates a JSONArray with no values. / public JSONArray() { this.values = new ArrayList<>(); }

put

public JSONArray put(int index, boolean value)

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.

put

public JSONArray put(int index, double value)

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.

put

public JSONArray put(int index, int value)

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.

put

public JSONArray put(int index, long value)

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.

put

public JSONArray put(int index, Object value)

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.

isNull

public boolean isNull(int index)

Returns true if this array has no value at index, or if its value is the null reference or JSONObject#NULL.

opt

public Object opt(int index)

Returns the value at index, or null if the array has no value at index.

remove

public Object remove(int index)

Removes and returns the value at index, or null if the array has no value at index.

getBoolean

public boolean getBoolean(int index)

Returns the value at index if it exists and is a boolean or can be coerced to a boolean.

optBoolean

public boolean optBoolean(int index)

Returns the value at index if it exists and is a boolean or can be coerced to a boolean. Returns false otherwise.

optBoolean

public boolean optBoolean(int index, boolean fallback)

Returns the value at index if it exists and is a boolean or can be coerced to a boolean. Returns fallback otherwise.

getDouble

public double getDouble(int index)

Returns the value at index if it exists and is a double or can be coerced to a double.

optDouble

public double optDouble(int index)

Returns the value at index if it exists and is a double or can be coerced to a double. Returns NaN otherwise.


This documentation was auto-generated from the source code of microsphere-java.

Home

java-annotations

java-core

jdk-tools

lang-model

annotation-processor

java-test

Clone this wiki locally