We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Dear all interested readers,
I'm working with JSON for the first time, and found this instruction on the P5JS website:
var weather; function preload() { var url = 'http://api.openweathermap.org/data/2.5/weather?q=London,UK'; weather = loadJSON(url); }
function setup() { noLoop(); }
function draw() { background(200); // get the humidity value out of the loaded JSON var humidity = weather.main.humidity; fill(0, humidity); // use the humidity value to set the alpha ellipse(width/2, height/2, 50, 50); }
It gets the values for:
But now, I want to get the values from this url:
https://www.rijksmuseum.nl/api/nl/collection?q=Delfts blauw&key=fpGQTuED&format=json
first value I want to get:
"url": "http://lh6.ggpht.com/5uBzvjv5x3kYBP5I4k193sxz9VaLNm4Ga6NpUlQY7FKUUCVGE2VLgaifqjbGC5s7OHNZZVpK9z62BnlLuJ5Jb54BlQ=s0"
},
second value I want to get:
"title": "Een moeder die het haar van haar kind reinigt, bekend als ‘Moedertaak’",
So I tried "var humidity = weather. artObjects. webImage. url;" but it doesn't work. What am I missing? Can I also only get the first [0] value? I don't understand how to use it.
Thanks in advance for your reply.
Answers
That weather... won't work; you have to use the data structure of the rijksmuseum which is different from weather
Maybe load it with loadStrings as String and println() and post here
Also see How to format code and text
@joshuakoomen: JSON is a very simple data structure. A good way to understand the data you're working with is to copy/paste it into something like http://www.jsoneditoronline.org/; then click the right arrow to see a structured view. In this case you have a parent {object} with 3 top level properties; where artObjects is an [array] with 12 elements.
Assuming you assign the object returned by loadJSON to a variable
data
then to get at an artObjects image URL I would expect the following to work:data.artObjects[0].webImage.url
Where 0 is the first element in the artObjects array... Simply replace this with the number of the element you want to target:
data.artObjects[9].webImage.url