Static p5_sys::global::loadJSON[][src]

pub static loadJSON: LoadJsonInternalType
Expand description

Loads a JSON file from a file or a URL, and returns an Object. Note that even if the JSON file contains an Array, an Object will be returned with index numbers as keys.

This method is asynchronous, meaning it may not finish before the next line in your sketch is executed. JSONP is supported via a polyfill and you can pass in as the second argument an object with definitions of the json callback following the syntax specified here.

This method is suitable for fetching files up to size of 64MB.

Examples

// Examples use USGS Earthquake API:
//   https://earthquake.usgs.gov/fdsnws/event/1/#methods
let earthquakes;
function preload() {
  // Get the most recent earthquake in the database
  let url =
   'https://earthquake.usgs.gov/earthquakes/feed/v1.0/' +
    'summary/all_day.geojson';
  earthquakes = loadJSON(url);
}

function setup() {
  noLoop();
}

function draw() {
  background(200);
  // Get the magnitude and name of the earthquake out of the loaded JSON
  let earthquakeMag = earthquakes.features[0].properties.mag;
  let earthquakeName = earthquakes.features[0].properties.place;
  ellipse(width / 2, height / 2, earthquakeMag * 10, earthquakeMag * 10);
  textAlign(CENTER);
  text(earthquakeName, 0, height - 30, width, 30);
}
function setup() {
  noLoop();
  let url =
   'https://earthquake.usgs.gov/earthquakes/feed/v1.0/' +
    'summary/all_day.geojson';
  loadJSON(url, drawEarthquake);
}

function draw() {
  background(200);
}

function drawEarthquake(earthquakes) {
  // Get the magnitude and name of the earthquake out of the loaded JSON
  let earthquakeMag = earthquakes.features[0].properties.mag;
  let earthquakeName = earthquakes.features[0].properties.place;
  ellipse(width / 2, height / 2, earthquakeMag * 10, earthquakeMag * 10);
  textAlign(CENTER);
  text(earthquakeName, 0, height - 30, width, 30);
}

Overloads

path name of the file or url to load

jsonpOptions? options object for jsonp related settings

datatype? “json” or “jsonp”

callback? function to be executed after loadJSON() completes, data is passed in as first argument

errorCallback? function to be executed if there is an error, response is passed in as first argument


path name of the file or url to load

datatype “json” or “jsonp”

callback? function to be executed after loadJSON() completes, data is passed in as first argument

errorCallback? function to be executed if there is an error, response is passed in as first argument


path name of the file or url to load

callback function to be executed after loadJSON() completes, data is passed in as first argument

errorCallback? function to be executed if there is an error, response is passed in as first argument