diff --git a/java/examples/Topics/Advanced Data/LoadSaveJSON/LoadSaveJSON.pde b/java/examples/Topics/Advanced Data/LoadSaveJSON/LoadSaveJSON.pde index 06f6e20d2..9c8700608 100644 --- a/java/examples/Topics/Advanced Data/LoadSaveJSON/LoadSaveJSON.pde +++ b/java/examples/Topics/Advanced Data/LoadSaveJSON/LoadSaveJSON.pde @@ -8,34 +8,32 @@ * * Here is what the JSON looks like (partial): * -{ - "bubbles": { - "bubble": [ - { - "position": { - "x": 160, - "y": 103 - }, - "diameter": 43.19838, - "label": "Happy" - }, - { - "position": { - "x": 121, - "y": 179 - }, - "diameter": 44.758068, - "label": "Melancholy" - } - ] - } -} + { + "bubbles": [ + { + "position": { + "x": 160, + "y": 103 + }, + "diameter": 43.19838, + "label": "Happy" + }, + { + "position": { + "x": 372, + "y": 137 + }, + "diameter": 52.42526, + "label": "Sad" + } + ] + } */ - + // An Array of Bubble objects Bubble[] bubbles; -// A Table object -JSONArray json; +// A JSON object +JSONObject json; void setup() { size(640, 360); @@ -45,88 +43,78 @@ void setup() { 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); + 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"); + String jsonString = join(loadStrings("data.json"), "\n"); //println(jsonString); - - json = JSONArray.parse(jsonString); - println(json); - - // Get all the child nodes named "bubble" -// XML[] children = xml.getChildren("bubble"); -// -// // The size of the array of Bubble objects is determined by the total XML elements named "bubble" -// bubbles = new Bubble[children.length]; -// -// for (int i = 0; i < bubbles.length; i++) { -// -// // The position element has two attributes: x and y -// XML positionElement = children[i].getChild("position"); -// // Note how with attributes we can get an integer or float directly -// float x = positionElement.getInt("x"); -// float y = positionElement.getInt("y"); -// -// // The diameter is the content of the child named "diamater" -// XML diameterElement = children[i].getChild("diameter"); -// // Note how with the content of an XML node, we retrieve as a String and then convert -// float diameter = float(diameterElement.getContent()); -// -// // The label is the content of the child named "label" -// XML labelElement = children[i].getChild("label"); -// String label = labelElement.getContent(); -// -// // Make a Bubble object out of the data read -// bubbles[i] = new Bubble(x, y, diameter, label); -// } + json = JSONObject.parse(jsonString); + println(json); + + 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.size()]; + + for (int i = 0; i < bubbleData.size(); 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);*/ + } } // 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(); + // 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/LoadSaveJSON/data/data.json b/java/examples/Topics/Advanced Data/LoadSaveJSON/data/data.json index 6e0449866..bcb0079c3 100644 --- a/java/examples/Topics/Advanced Data/LoadSaveJSON/data/data.json +++ b/java/examples/Topics/Advanced Data/LoadSaveJSON/data/data.json @@ -1,6 +1,5 @@ { - "bubbles": { - "bubble": [ + "bubbles": [ { "position": { "x": 160, @@ -34,5 +33,4 @@ "label": "Melancholy" } ] - } } \ No newline at end of file diff --git a/java/examples/Topics/Advanced Data/LoadSaveJSONTest/LoadSaveJSONTest.pde b/java/examples/Topics/Advanced Data/LoadSaveJSONTest/LoadSaveJSONTest.pde index df55a3db9..46b4a1e7f 100644 --- a/java/examples/Topics/Advanced Data/LoadSaveJSONTest/LoadSaveJSONTest.pde +++ b/java/examples/Topics/Advanced Data/LoadSaveJSONTest/LoadSaveJSONTest.pde @@ -36,7 +36,7 @@ import org.json.JSONObject; // An Array of Bubble objects Bubble[] bubbles; -// A Table object +// A JSON object JSONObject json; void setup() {