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

JSONArray

Name

getJSONArray()

Examples
// The following short JSON file called "data.json" is parsed 
// in the code below. It must be in the project's "data" folder.
//
// [
//   [
//     { "name": "apple", "isFruit": true },
//     { "name": "grape", "isFruit": true },
//     { "name": "carrot", "isFruit": false }
//   ],
//   [
//     { "name": "lettuce", "isFruit": false },
//     { "name": "plum", "isFruit": true },
//     { "name": "cinnamon", "isFruit": false }
//   ]
// ]

JSONArray json;

void setup() {

  json = loadJSONArray("data.json");

  // Get the first array of elements
  JSONArray values = json.getJSONArray(0);
  
  for (int i = 0; i < values.size(); i++) {
    
    JSONObject item = values.getJSONObject(i); 

    String name = item.getString("name");
    boolean isFruit = item.getBoolean("isFruit");

    println(name + ", " + isFruit);
  }
}

// Sketch prints:
// apple, true
// grape, true
// carrot, false
Description Retrieves the JSONArray with the associated index value.
Syntax
.getJSONArray(index)
.getJSONArray(index, defaultValue)
Parameters
index int: must be between 0 and length() - 1
ReturnsJSONArray
RelatedgetJSONObject()
setJSONObject()
setJSONArray()
Updated on January 21, 2019 10:05:13am EST

Creative Commons License