diff --git a/README.md b/README.md index 06bb7fb55..3f6ed2010 100644 --- a/README.md +++ b/README.md @@ -4,11 +4,11 @@ This library is simply a fork of the original JSON for [Java library written by # Since Processing 2.0 beta 8 -Since the beta release (8) of Processing 2.0 (February 24) JSON is part of the core libraries; to avoid any ambiguity problems you should upgrade to [the latest](http://santiclaws.se/json4processing/json4processing-0.1.7.zip) version of this library. +Since the beta release (8) of Processing 2.0 (February 24) JSON is part of the core libraries; to avoid any ambiguity problems you should upgrade to [the latest](http://santiclaws.se/json4processing/json4processing-0.1.8.zip) version of this library. # Installation in Processing -1. Download the latest version [here](http://santiclaws.se/json4processing/json4processing-0.1.7.zip) +1. Download the latest (0.1.8) version [here](http://santiclaws.se/json4processing/json4processing-0.1.8.zip) 2. Extract the zip-file into your /sketchbook/libraries/ folder. 3. Restart Processing IDE @@ -97,6 +97,29 @@ arr.append(third); println(arr); ``` +**Parson JSON from websource** + +``` java +/** + * Creating a JSON array of objects + */ +JSON first = JSON.createObject(); +first.setInt("val", 5); + +JSON sec = JSON.createObject(); +sec.setFloat("val", 5.5); + +JSON third = JSON.createObject(); +third.setString("val", "a"); + +JSON arr = JSON.createArray(); +arr.append(first); +arr.append(sec); +arr.append(third); + +println(arr); +``` + # Original README JSON in Java [package org.json] @@ -166,4 +189,4 @@ XML.java: XML provides support for converting between JSON and XML. JSONML.java: JSONML provides support for converting between JSONML and XML. -XMLTokener.java: XMLTokener extends JSONTokener for parsing XML text. \ No newline at end of file +XMLTokener.java: XMLTokener extends JSONTokener for parsing XML text. diff --git a/examples/OpenSignal/OpenSignal.pde b/examples/OpenSignal/OpenSignal.pde new file mode 100644 index 000000000..056fd4379 --- /dev/null +++ b/examples/OpenSignal/OpenSignal.pde @@ -0,0 +1,48 @@ +/* + * JSON 4 Processing + * Basic example: Parsing data from OpenSignal API + * + * Get your own API key from https://opensignal.3scale.net/login + */ + +import org.json.*; + +String cid = "10132"; +String lac = "9015"; +String sid = "0"; +String phone_type = "GSM"; +String network_id = "24001"; +String api_key = ""; + +PFont font; + +void setup(){ + font = loadFont("Ubuntu-24.vlw"); + textFont(font, 12); + fill( 0 ); + + // 1. Create the URL + String url = "http://api.opensignal.com/v2/towerinfo.json?cid="+cid+"&lac="+lac+"&sid="+sid+"&phone_type="+phone_type+"&network_id="+network_id+"&apikey="+api_key; + + // 2. Get the json-formatted string + String[] jsonstring = loadStrings(url); + + // 3. Initialize the object + JSON cell_tower = JSON.parse(jsonstring[0]); + + println( cell_tower ); + + showInformation( cell_tower.getJSON("tower1") ); +} + +void draw(){ +} + +void showInformation(JSON tower){ + translate( 5, 12 ); + text("Cell tower", 0, 0); + text("LAC: " + tower.getString("lac"), 0, 12); + text("ID: " + tower.getString("cid"), 0, 24); + text("lat: " + tower.getString("est_lat"), 0, 36); + text("lng: " + tower.getString("est_lng"), 0, 48); +} \ No newline at end of file diff --git a/examples/OpenSignal/data/Ubuntu-24.vlw b/examples/OpenSignal/data/Ubuntu-24.vlw new file mode 100644 index 000000000..cda9da601 Binary files /dev/null and b/examples/OpenSignal/data/Ubuntu-24.vlw differ diff --git a/examples/convert_xml_to_json/convert_xml_to_json.pde b/examples/convert_xml_to_json/convert_xml_to_json.pde index 3b62a246e..62777cff1 100644 --- a/examples/convert_xml_to_json/convert_xml_to_json.pde +++ b/examples/convert_xml_to_json/convert_xml_to_json.pde @@ -21,7 +21,7 @@ PFont font; void setup() { size(600, 360); - font = createFont("Merriweather-Light.ttf", 28); + font = createFont("Merriweather-Light.vlw", 28); textFont(font); // The URL for the XML document @@ -52,7 +52,6 @@ void draw() { // Display all the stuff we want to display text("Zip code: " + zip, width*0.15, height*0.33); - text("Today’s high: " + temperature, width*0.15, height*0.5); + text("Todays high: " + temperature, width*0.15, height*0.5); text("Forecast: " + weather, width*0.15, height*0.66); - } \ No newline at end of file diff --git a/src/org/json/JSON.java b/src/org/json/JSON.java index f8bb31704..8ef651540 100644 --- a/src/org/json/JSON.java +++ b/src/org/json/JSON.java @@ -29,7 +29,11 @@ * able of calling ".get(index)" on an JSONObject for example... it should then * notify the user by a simple text message to the console. * - * @author ksango + * This library started from the JSONObject and JSONArray classes by Douglas + * Crockford, since then it has been heavily modified and also includes changes + * from the JSONArray and JSONObject classes from Processing core. + * + * @author Andreas Goransson * */ public class JSON { @@ -143,9 +147,9 @@ public static JSON createArray(){ /** * Open a json file * - * @param json + * @param json filename * - * @return + * @return JSON (array or object) */ public static JSON load(String filename) { InputStream input = null; @@ -171,6 +175,13 @@ public static JSON load(String filename) { throw new RuntimeException("File is not JSON formatted"); } + /** + * Parse a JSON string + * + * @param data json formatted string + * + * @return JSON (array or object) + */ public static JSON parse(String data){ JSONTokener tokener = new JSONTokener(data);