Json with processing.js

edited June 2014 in JavaScript Mode

Hi, I tried using Pomax example with Json, but without succes. The code used is here http://processingjs.org/articles/PomaxGuide.html#json . The only thing I modified is that

var json = $.get("http://thatfunkysite.com/serving/json.asp"); becomes

var json = $.get("/path/to/points.json");

What else should I change? I looked everywhere on the internet and couldn't find a solution.

I also came across this piece of code, which works fine as .pde, but doesn't work as processing.js

// Declare some global variables
JSONObject response;
String videoTitle; 

void setup() {
  // Load the JSON file as a JSON object
  response = loadJSONObject("response.json");

  // Get the array of items from the JSON object
  JSONArray items = response.getJSONArray("items");

  // Get the first item in the JSON array
  JSONObject firstVideo = items.getJSONObject(0);

  // Get a JSON Object with a list of details about the first item
  JSONObject snippet = firstVideo.getJSONObject("snippet");

  // Get title the string from final JSON object
  videoTitle = snippet.getString("title");

  // Print the title of the first video result

I simply need a simple, working code that deals with JSON in processing.js

Thank you!


  • edited June 2014 Answer ✓

    When Processing 2's come out, it brought along some new data structures like JSONObject, Table, IntList, etc.
    And renamed XMLElement to just XML and raised its # of methods.

    Old Processing 1.5.1 had only XMLElement as an "exclusive" data structure class apart from some Java's built-in 1s!

    But why I'm telling you all these? B/c the corresponding PJS library is still pretty much similar to P5 1.5.1!
    We still can't use the newer classes & methods from P5 2.x.x series and on in "JavaScript Mode"!

  • Thank you so much! I will use XMLelement for playing with my data. It;s just as good with what I need :) I tried the example here http://processingjs.org/reference/XMLElement/ and it's perfect :)

  • Glad XML is as good as JSON for ya! :D
    Just remember that you can still test it in "Java Mode" if you rename XMLElement as XML! ;)

Sign In or Register to comment.