Skip to content

Instantly share code, notes, and snippets.

@nasser
Last active February 13, 2016 19:17
Show Gist options
  • Save nasser/dc6c9b25628426acfe32 to your computer and use it in GitHub Desktop.
Save nasser/dc6c9b25628426acfe32 to your computer and use it in GitHub Desktop.
Downloading and Parsing JSON
// node.js
// use the built in http library
var http = require("http");
// start a GET request for the URL
http.get("http://marsweather.ingenology.com/v1/latest/",
function(result) {
// when the request is ready, start downloading the data in chunks
var data = "";
result.on("data", function (chunk) {
// when a chunk of data arrives, add it to the data string
data += chunk.toString();
});
result.on("end", function () {
// when there are no more chunks of data, parse the json and print it out
var json = JSON.parse(data);
console.log(json.report.sunrise);
});
});
// processing
// declare a variable to store the json
JSONObject json;
void setup() {
size(800, 800);
// request the json data from the URL, loading it as json in the same step
json = loadJSONObject("http://marsweather.ingenology.com/v1/latest/?format=json");
// extract a value from the json data
float pressure = json.getJSONObject("report").getFloat("pressure");
// use the value to draw to the screen
ellipse(500.0, 500.0, pressure, pressure);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment