Getting the right JSON value

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:


{"coord":{"lon":-0.13,"lat":51.51},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}],"base":"stations","main":{"temp":288.33,"pressure":1019,"humidity":67,"temp_min":286.15,"temp_max":290.93},"visibility":10000,"wind":{"speed":1},"clouds":{"all":36},"dt":1438500869,"sys":{"type":1,"id":5091,"message":0.015,"country":"GB","sunrise":1438489535,"sunset":1438544817},"id":2643743,"name":"London","cod":200}

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.

Tagged:

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

  • Answer ✓

    @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

Sign In or Register to comment.