From 24da25497f21abded5d7b04b06a9bf8ba6731f97 Mon Sep 17 00:00:00 2001 From: Daniel Shiffman Date: Tue, 30 Apr 2013 22:05:37 -0400 Subject: [PATCH] updating JSON example --- .../LoadSaveJSON/LoadSaveJSON.pde | 11 +- .../Advanced Data/LoadSaveJSONTest/Bubble.pde | 40 ------ .../LoadSaveJSONTest/LoadSaveJSONTest.pde | 126 ------------------ .../LoadSaveJSONTest/data/data.json | 36 ----- 4 files changed, 5 insertions(+), 208 deletions(-) delete mode 100644 java/examples/Topics/Advanced Data/LoadSaveJSONTest/Bubble.pde delete mode 100644 java/examples/Topics/Advanced Data/LoadSaveJSONTest/LoadSaveJSONTest.pde delete mode 100644 java/examples/Topics/Advanced Data/LoadSaveJSONTest/data/data.json diff --git a/java/examples/Topics/Advanced Data/LoadSaveJSON/LoadSaveJSON.pde b/java/examples/Topics/Advanced Data/LoadSaveJSON/LoadSaveJSON.pde index 83badb3c8..7e2b80574 100644 --- a/java/examples/Topics/Advanced Data/LoadSaveJSON/LoadSaveJSON.pde +++ b/java/examples/Topics/Advanced Data/LoadSaveJSON/LoadSaveJSON.pde @@ -55,7 +55,7 @@ void draw() { void loadData() { // Load JSON file // Temporary full path until path problem resolved. - json = loadJSONObject("/Users/shiffman/Desktop/LoadSaveJSON/data/data.json"); + json = loadJSONObject("data.json"); JSONArray bubbleData = json.getJSONArray("bubbles"); @@ -64,7 +64,7 @@ void draw() { for (int i = 0; i < bubbleData.size(); i++) { // Get each object in the array - JSONObject bubble = bubbleData.getObject(i); + JSONObject bubble = bubbleData.getJSONObject(i); // Get a position object JSONObject position = bubble.getJSONObject("position"); // Get x,y from position @@ -104,9 +104,8 @@ void draw() { bubbleData.removeIndex(0); } - println(json); - // Not implemented yet - //saveJSONObject(json,"data.json"); - //loadData(); + // Save new data + saveJSONObject(json,"data/data.json"); + loadData(); } diff --git a/java/examples/Topics/Advanced Data/LoadSaveJSONTest/Bubble.pde b/java/examples/Topics/Advanced Data/LoadSaveJSONTest/Bubble.pde deleted file mode 100644 index 3a7b9b7f9..000000000 --- a/java/examples/Topics/Advanced Data/LoadSaveJSONTest/Bubble.pde +++ /dev/null @@ -1,40 +0,0 @@ -// A Bubble class - -class Bubble { - float x,y; - float diameter; - String name; - - boolean over = false; - - // Create the Bubble - Bubble(float x_, float y_, float diameter_, String s) { - x = x_; - y = y_; - diameter = diameter_; - name = s; - } - - // CHecking if mouse is over the Bubble - void rollover(float px, float py) { - float d = dist(px,py,x,y); - if (d < diameter/2) { - over = true; - } else { - over = false; - } - } - - // Display the Bubble - void display() { - stroke(0); - strokeWeight(2); - noFill(); - ellipse(x,y,diameter,diameter); - if (over) { - fill(0); - textAlign(CENTER); - text(name,x,y+diameter/2+20); - } - } -} diff --git a/java/examples/Topics/Advanced Data/LoadSaveJSONTest/LoadSaveJSONTest.pde b/java/examples/Topics/Advanced Data/LoadSaveJSONTest/LoadSaveJSONTest.pde deleted file mode 100644 index 46b4a1e7f..000000000 --- a/java/examples/Topics/Advanced Data/LoadSaveJSONTest/LoadSaveJSONTest.pde +++ /dev/null @@ -1,126 +0,0 @@ -/** - * Loading XML Data - * by Daniel Shiffman. - * - * This example demonstrates how to use loadJSON() - * to retrieve data from a JSON file and make objects - * from that data. - * - * Here is what the JSON looks like (partial): - * -{ - "bubbles": [ - { - "position": { - "x": 160, - "y": 103 - }, - "diameter": 43.19838, - "label": "Happy" - }, - { - "position": { - "x": 372, - "y": 137 - }, - "diameter": 52.42526, - "label": "Sad" - } - ] -} - */ - -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; - -// An Array of Bubble objects -Bubble[] bubbles; -// A JSON object -JSONObject json; - -void setup() { - size(640, 360); - loadData(); -} - -void draw() { - background(255); - // Display all bubbles - for (Bubble b : bubbles) { - b.display(); - b.rollover(mouseX, mouseY); - } - // - // textAlign(LEFT); - // fill(0); - // text("Click to add bubbles.", 10, height-10); -} - -void loadData() { - // Load JSON file - String jsonString = join(loadStrings("data.json"), "\n"); - //println(jsonString); - - try { - json = new JSONObject(jsonString); - JSONArray bubbleData = json.getJSONArray("bubbles"); - - // The size of the array of Bubble objects is determined by the total XML elements named "bubble" - bubbles = new Bubble[bubbleData.length()]; - - for (int i = 0; i < bubbleData.length(); i++) { - JSONObject bubble = bubbleData.getJSONObject(i); - JSONObject position = bubble.getJSONObject("position"); - int x = position.getInt("x"); - int y = position.getInt("y"); - - float diameter = (float)bubble.getDouble("diameter"); - String label = bubble.getString("label"); - - bubbles[i] = new Bubble(x, y, diameter, label); - } - } - catch (JSONException e) { - e.printStackTrace(); - } -} - -// Still need to work on adding and deleting - -void mousePressed() { - - // Create a new XML bubble element - // XML bubble = xml.addChild("bubble"); - // - // // Set the poisition element - // XML position = bubble.addChild("position"); - // // Here we can set attributes as integers directly - // position.setInt("x",mouseX); - // position.setInt("y",mouseY); - // - // // Set the diameter element - // XML diameter = bubble.addChild("diameter"); - // // Here for a node's content, we have to convert to a String - // diameter.setContent("" + random(40,80)); - // - // // Set a label - // XML label = bubble.addChild("label"); - // label.setContent("New label"); - // - // - // // Here we are removing the oldest bubble if there are more than 10 - // XML[] children = xml.getChildren("bubble"); - // // If the XML file has more than 10 bubble elements - // if (children.length > 10) { - // // Delete the first one - // xml.removeChild(children[0]); - // } - // - // // Save a new XML file - // saveXML(xml,"data/data.xml"); - // - // // reload the new data - // loadData(); -} - diff --git a/java/examples/Topics/Advanced Data/LoadSaveJSONTest/data/data.json b/java/examples/Topics/Advanced Data/LoadSaveJSONTest/data/data.json deleted file mode 100644 index bcb0079c3..000000000 --- a/java/examples/Topics/Advanced Data/LoadSaveJSONTest/data/data.json +++ /dev/null @@ -1,36 +0,0 @@ -{ - "bubbles": [ - { - "position": { - "x": 160, - "y": 103 - }, - "diameter": 43.19838, - "label": "Happy" - }, - { - "position": { - "x": 372, - "y": 137 - }, - "diameter": 52.42526, - "label": "Sad" - }, - { - "position": { - "x": 273, - "y": 235 - }, - "diameter": 61.14072, - "label": "Joyous" - }, - { - "position": { - "x": 121, - "y": 179 - }, - "diameter": 44.758068, - "label": "Melancholy" - } - ] -} \ No newline at end of file