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.

Class

JSONObject

Name

setJSONArray()

Examples
String[] species = { "Capra hircus", "Panthera pardus", "Equus zebra" };
String[] names = { "Goat", "Leopard", "Zebra" };

JSONObject json;

void setup() {
  
  JSONArray 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);
  }
  
  json = new JSONObject();
  json.setJSONArray("animals", values);

  saveJSONObject(json, "data/new.json");
}

// Sketch saves the following to a file called "new.json":
// {"animals": [
//   {
//     "id": 0,
//     "species": "Capra hircus",
//     "name": "Goat"
//   },
//   {
//     "id": 1,
//     "species": "Panthera pardus",
//     "name": "Leopard"
//   },
//   {
//     "id": 2,
//     "species": "Equus zebra",
//     "name": "Zebra"
//   }
// ]}
Description Sets the value of the JSONArray with the associated key.
Syntax
.setJSONArray(key, value)
Parameters
key String: a key string
value JSONArray: value to assign
ReturnsJSONObject
RelatedsetJSONObject()
getJSONObject()
getJSONArray()
Updated on January 21, 2019 10:05:13am EST

Creative Commons License