This reference is for Processing 3.0+. If you have a previous version, use the reference included with your software in the Help menu. If you see any errors or have suggestions, please let us know. If you prefer a more technical reference, visit the Processing Core Javadoc and Libraries Javadoc.



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"
//   }
// ]
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.
getString() Gets the String value associated with an index
getInt() Gets the int value associated with an index
getFloat() Gets the float value associated with an index
getBoolean() Gets the boolean value associated with an index
getJSONArray() Gets the JSONArray associated with an index value
getJSONObject() Gets the JSONObject associated with an index value
getStringArray() Gets the entire array as an array of Strings
getIntArray() Gets the entire array as array of ints
append() Appends a value, increasing the array's length by one
setString() Put a String value in the JSONArray
setInt() Put an int value in the JSONArray
setFloat() Put a float value in the JSONArray
setBoolean() Put a boolean value in the JSONArray
setJSONArray() Sets the JSONArray value associated with an index value
setJSONObject() Sets the JSONObject value associated with an index value
size() Gets the number of elements in the JSONArray
isNull() Determine if the value is null.
remove() Removes an element
Updated on August 17, 2018 03:36:34pm EDT

Creative Commons License