We are about to switch to a new forum software. Until then we have removed the registration on this forum.
This is a ground-zero type of question but I'm stumped. I am experimenting with how to use data while using p5.js. I chose a random JSON file containing different fictional religions and am attempting to display that in a window. So far I've only got errors, blank screens, and individual bits of information to show. I've tried to create an array with this data but it hasn't worked out. I'm very new, excuse my inability to speak in programmer language here. Thanks so much for looking it over.
Here's the code:
var data;
function preload() {
data = loadJSON("flower.json");
}
function setup() {
noCanvas();
var flower = data.religion;
for( i = 0; i < flower.length; i++) {
createElement('h1', flower[i].religion)
}
}
My JSON file (flower.json) looks like this (I'm only giving a sample because it's huge, 255+ entries):
{
"religion": [
{
"Arceusism": "worshippers of Arceus, the creator of all Pokémon. See also Giratinism",
"The Babarambaba Cult": "worshippers of Mother Nature, personified in a head of lettuce named Ralph — \"Rerun Sees the Light\" episode of What's Happening!!",
"Bajoran religion": "Star Trek: Deep Space Nine",
"Bandosianism": "RuneScape's war religion",
"Banjoism": "Elan's religion in The Order of the Stick",
"Beism": "Giles Goat-Boy, John Barth",
"Beliar": "Gothic (video game), god of darkness, death and chaos and lord of lightning and earth.",
"Bene Gesserit": "Dune series"
} ]
}
Answers
Oh dear, what happened to my formatting? Apologies.
Edit post, highlight text, hit ctrl-o.
Can do the same for the data too
My gosh, that answered one question but not the entire thing! Yeah, I'm new.
http://Bl.ocks.org/GoSubRoutine/9aa9cd00000e331612c8e557b5dd28f0
var flower = data.religion;
is lacking something. :-?const flower = data.religion[0];
L-)for ( in ) {}
loop. :-bdhttps://developer.Mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in
I did get it to work on my own (first successful dive into Google to fix a program!) but I'm going to unpack your solution as well for confirmation. Thanks so much!
What worked for me:
For the JSON file, I simply took out the "religion": [ { } ] part and left only the original text.
@GoToLoop: I tried using your exact code and nothing appears in the window. I'm using p5.js for this. Could it have to do with not important the correct library?
Visiting the online sketch at:
http://Bl.ocks.org/GoSubRoutine/9aa9cd00000e331612c8e557b5dd28f0
We can see the "index.html" used for kickstarting "sketch.js" there:
In order to use createP(), we also need to import "p5.dom.js" after "p5.js": L-)
https://p5js.org/reference/#/p5/createP