File tree Expand file tree Collapse file tree 4 files changed +64
-6
lines changed
Expand file tree Collapse file tree 4 files changed +64
-6
lines changed Original file line number Diff line number Diff line change 1+ /*
2+ * JSON 4 Processing
3+ * Basic example: Parsing data from OpenSignal API
4+ *
5+ * Get your own API key from https://opensignal.3scale.net/login
6+ */
7+
8+ import org.json.* ;
9+
10+ String cid = " 10132" ;
11+ String lac = " 9015" ;
12+ String sid = " 0" ;
13+ String phone_type = " GSM" ;
14+ String network_id = " 24001" ;
15+ String api_key = " <your api key>" ;
16+
17+ PFont font;
18+
19+ void setup (){
20+ font = loadFont (" Ubuntu-24.vlw" );
21+ textFont (font, 12 );
22+ fill ( 0 );
23+
24+ // 1. Create the URL
25+ 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;
26+
27+ // 2. Get the json-formatted string
28+ String [] jsonstring = loadStrings (url);
29+
30+ // 3. Initialize the object
31+ JSON cell_tower = JSON . parse(jsonstring[0 ]);
32+
33+ println ( cell_tower );
34+
35+ showInformation( cell_tower. getJSON(" tower1" ) );
36+ }
37+
38+ void draw (){
39+ }
40+
41+ void showInformation (JSON tower ){
42+ translate ( 5 , 12 );
43+ text (" Cell tower" , 0 , 0 );
44+ text (" LAC: " + tower. getString(" lac" ), 0 , 12 );
45+ text (" ID: " + tower. getString(" cid" ), 0 , 24 );
46+ text (" lat: " + tower. getString(" est_lat" ), 0 , 36 );
47+ text (" lng: " + tower. getString(" est_lng" ), 0 , 48 );
48+ }
Original file line number Diff line number Diff line change @@ -21,7 +21,7 @@ PFont font;
2121void setup () {
2222 size (600 , 360 );
2323
24- font = createFont (" Merriweather-Light.ttf " , 28 );
24+ font = createFont (" Merriweather-Light.vlw " , 28 );
2525 textFont (font);
2626
2727 // The URL for the XML document
@@ -52,7 +52,6 @@ void draw() {
5252
5353 // Display all the stuff we want to display
5454 text (" Zip code: " + zip, width * 0.15 , height * 0.33 );
55- text (" Today’s high: " + temperature, width * 0.15 , height * 0.5 );
55+ text (" Todays high: " + temperature, width * 0.15 , height * 0.5 );
5656 text (" Forecast: " + weather, width * 0.15 , height * 0.66 );
57-
5857}
Original file line number Diff line number Diff line change 2929 * able of calling ".get(index)" on an JSONObject for example... it should then
3030 * notify the user by a simple text message to the console.
3131 *
32- * @author ksango
32+ * This library started from the JSONObject and JSONArray classes by Douglas
33+ * Crockford, since then it has been heavily modified and also includes changes
34+ * from the JSONArray and JSONObject classes from Processing core.
35+ *
36+ * @author Andreas Goransson
3337 *
3438 */
3539public class JSON {
@@ -143,9 +147,9 @@ public static JSON createArray(){
143147 /**
144148 * Open a json file
145149 *
146- * @param json
150+ * @param json filename
147151 *
148- * @return
152+ * @return JSON (array or object)
149153 */
150154 public static JSON load (String filename ) {
151155 InputStream input = null ;
@@ -171,6 +175,13 @@ public static JSON load(String filename) {
171175 throw new RuntimeException ("File is not JSON formatted" );
172176 }
173177
178+ /**
179+ * Parse a JSON string
180+ *
181+ * @param data json formatted string
182+ *
183+ * @return JSON (array or object)
184+ */
174185 public static JSON parse (String data ){
175186 JSONTokener tokener = new JSONTokener (data );
176187
You can’t perform that action at this time.
0 commit comments