Reference+
Class Name
JSONArray
Description
A JSONArray stores an array of JSON objects. JSONArrays can be generated from scratch, dynamically, or using data from an existing file. JSON can also be output and saved to disk, as in the example above.
Examples
String[] species = { "Capra hircus", "Panthera pardus", "Equus zebra" }; String[] names = { "Goat", "Leopard", "Zebra" }; JSONArray values; void setup() { values = new JSONArray(); for (int i = 0; i < species.length; i++) { JSONObject animal = new JSONObject(); animal.setInt("id", i); animal.setString("species", species[i]); animal.setString("name", names[i]); values.setJSONObject(i, animal); } saveJSONArray(values, "data/new.json"); } // Sketch saves the following to a file called "new.json": // [ // { // "id": 0, // "species": "Capra hircus", // "name": "Goat" // }, // { // "id": 1, // "species": "Panthera pardus", // "name": "Leopard" // }, // { // "id": 2, // "species": "Equus zebra", // "name": "Zebra" // } // ]
Constructors
JSONArray()
Methods
getString()
Gets the String value associated with an indexgetInt()
Gets the int value associated with the specified indexgetFloat()
Gets the float value associated with the specified indexgetBoolean()
Gets the boolean value associated with the specified indexgetJSONArray()
Retrieves the JSONArray with the associated index valuegetJSONObject()
Retrieves the JSONObject with the associated index valuetoStringArray()
Returns the entire JSONArray as an array of StringstoIntArray()
Returns the entire JSONArray as an array of intsappend()
Appends a value, increasing the array's length by onesetString()
Inserts a new value into the JSONArray at the specified index positionsetInt()
Put an int value in the JSONArraysetFloat()
Put a float value in the JSONArraysetBoolean()
Inserts a new value into the JSONArray at the specified index positionsetJSONArray()
Sets the value of the JSONArray with the associated index valuesetJSONObject()
Sets the value of the JSONObject with the index valuesize()
Gets the total number of elements in a JSONArrayisNull()
Determines if the value associated with the index is nullremove()
Removes the element from a JSONArray in the specified index position
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.