Class Name

JSONObject

Description

A JSONObject stores JSON data with multiple name/value pairs. Values can be numeric, Strings, booleans, other JSONObjects or JSONArrays, or null. JSONObject and JSONArray objects are quite similar and share most of the same methods; the primary difference is that the latter stores an array of JSON objects, while the former represents a single JSON object.

JSON 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

  • JSONObject json;
    
    void setup() {
    
      json = new JSONObject();
    
      json.setInt("id", 0);
      json.setString("species", "Panthera leo");
      json.setString("name", "Lion");
    
      saveJSONObject(json, "data/new.json");
    }
    
    // Sketch saves the following to a file called "new.json":
    // {
    //   "id": 0,
    //   "species": "Panthera leo",
    //   "name": "Lion"
    // }
    

Methods

  • getString()Gets the String value associated with the specified key
  • getInt()Gets the int value associated with the specified key
  • getFloat()Gets the float value associated with a key
  • getBoolean()Gets the boolean value associated with the specified key
  • getJSONArray()Retrieves the JSONArray with the associated key
  • getJSONObject()Given a key value, retrieves the associated JSONObject
  • isNull()Determines if the value associated with the key is null, that is has no defined value (false) or if it has a value (true)
  • setString()Inserts a new key/String pair into the JSONObject
  • setInt()Inserts a new key/int pair into the JSONObject
  • setFloat()Put a key/float pair in the JSONObject
  • setBoolean()Put a key/boolean pair in the JSONObject
  • setJSONObject()Sets the value of the JSONObject with the associated key
  • setJSONArray()Sets the value of the JSONArray with the associated key