Lettura di un file JSON
// height=200 lines=auto
let terremoti;
function preload() {
terremoti = loadJSON( "assets/terremoti_200504_0240.json" );
}
function setup() {
createCanvas(600, 760);
background(255);
for (let i = 0; i < 50; i++) {
let info = terremoti.features[i].properties;
text( info.place + ": " + info.mag, 10, i*15+15 );
}
}
terremoti = loadJSON( "assets/terremoti_200504_0240.json" );
Carica un file con le informazioni lette dal sito "U.S. Geological Survey" sugli ultimi 500 terremoti al 04/05/2020 alle 02:40. Il valore restituito è un oggetto JavaScript strutturato con una variante del formato GeoJSON.
let info = terremoti.features[i].properties;
text( info.place + ": " + info.mag, 10, i*15+15 );
I dati di ogni terremoto sono memorizzati nell'array features. Ogni elemento dell'array ha un oggetto properties in cui sono memorizzate alcune informazioni come il luogo (place) e la magnitudo della scossa (mag).
Strutturazione dei file:
- CARTELLA-SKETCH
- assets
- libs
- p5.min.js
- index.html
- sketch.js