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 index
  • getInt()Gets the int value associated with the specified index
  • getFloat()Gets the float value associated with the specified index
  • getBoolean()Gets the boolean value associated with the specified index
  • getJSONArray()Retrieves the JSONArray with the associated index value
  • getJSONObject()Retrieves the JSONObject with the associated index value
  • toStringArray()Returns the entire JSONArray as an array of Strings
  • toIntArray()Returns the entire JSONArray as an array of ints
  • append()Appends a value, increasing the array's length by one
  • setString()Inserts a new value into the JSONArray at the specified index position
  • setInt()Put an int value in the JSONArray
  • setFloat()Put a float value in the JSONArray
  • setBoolean()Inserts a new value into the JSONArray at the specified index position
  • setJSONArray()Sets the value of the JSONArray with the associated index value
  • setJSONObject()Sets the value of the JSONObject with the index value
  • size()Gets the total number of elements in a JSONArray
  • isNull()Determines if the value associated with the index is null
  • remove()Removes the element from a JSONArray in the specified index position
Creative Commons License

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.