Simple JSON Example with API
in
Share your Work
•
1 year ago
Hello,
I wanted to share a Code with a simple API-request receiving JSON and parsing it.
It shows Concerts for each months in 2012 and you can move from one month to month by Cursor left and right. You can not scroll down within a month.
It might be that this line can be wrong for you with other APIs :
Uncomment it then. Just the variable "result" could be fine for you.
Words like items and titel and so on you have to change according to your API.
Greetings, Chrisir
I wanted to share a Code with a simple API-request receiving JSON and parsing it.
It shows Concerts for each months in 2012 and you can move from one month to month by Cursor left and right. You can not scroll down within a month.
It might be that this line can be wrong for you with other APIs :
- result= "{ \"success\": true, \"pagination\": { \"current\": 1, \"max\": 1 }, \"items\": " + result + "}" ;
Uncomment it then. Just the variable "result" could be fine for you.
Words like items and titel and so on you have to change according to your API.
Greetings, Chrisir
import org.json.*; String baseURL = "http://www.ekd.de/reformation-und-musik/api366+1.php?"; int intCurrentMonth = 3; PFont myFont; void setup() { size(800, 700); // (Un)comment the following two lines to see the available fonts //String[] fontList = PFont.list(); //println(fontList); // Create the font on the fly myFont = createFont("Sans Serif", 18); textFont(myFont); fill(0); //textMode (SCREEN); Output(); }; // func void draw() { // nothing here - // It's all in keyPressed() }; // func // =================================================== void Output() { // delete screen background (111); //THE MAIN METHOD GETS CALLED HERE // Ausgabe JSON getContentFromJSON(); // Ausgabe aktueller Monat text ( "Monat: " + trim(str(intCurrentMonth) + " (Cursor <>)"), width-170, 15); } // func // ================================================= void getContentFromJSON() { final int AbstandOben = 40; // oberer Rand final int AbstandItems = 112; // zwischen Blöcken final int AbstandZeilen = 19; // zwischen Zeilen innerhalb eines Blocks final int xPos = 22; // Text X Position int yPos = 0; // Text Y Position int ZeileNr; // Zeile Nummer innerhalb eines Blocks // with intCurrentMonth <= 12 the value of intCurrentMonth // is interpreted as a month String request = baseURL + "d=" + trim(str(intCurrentMonth)); String result = join( loadStrings( request ), ""); // println( result ); // String ergänzen result= "{ \"success\": true, \"pagination\": { \"current\": 1, \"max\": 1 }, \"items\": " + result + "}"; // try try { JSONObject Data1 = new JSONObject(result); JSONArray results = Data1.getJSONArray("items"); int numberOfElements = results.length(); // loop through array for (int i = 0; i < numberOfElements; i++) { JSONObject entry = results.getJSONObject(i); // headline textSize (18); yPos = i* AbstandItems +AbstandOben; text (entry.getString("titel") + " (" + entry.getString("nummer") + " von 366+1)", xPos, yPos ); // normaler Text ZeileNr=1; textSize (14); yPos = i* AbstandItems+ZeileNr*AbstandZeilen +AbstandOben; text (entry.getString("beschreibung") + " in " + entry.getString("ort"), xPos, yPos ); ZeileNr++; yPos = i* AbstandItems+ZeileNr*AbstandZeilen +AbstandOben; text (entry.getString("landeskirche") + ", " + entry.getString("landeskirche_url") + ", in " + entry.getString("bundesland"), xPos, yPos ); ZeileNr++; yPos = i* AbstandItems+ZeileNr*AbstandZeilen +AbstandOben; text ("Am " + getDate(entry.getString("datum")) + " in " + entry.getString("ort") + ", um " + getTime(entry.getString("zeit")) + " Uhr.", xPos, yPos ); // if yPos of the text is > Screen height if (yPos > height) { // leave for (to save programs executing time) break; } // if // } // for } // try catch (JSONException e) { println ("There was an error parsing the JSONObject."); } // catch } // func // ---------------------------------------------------------------------- String getTime( String time1 ) { // take care of time // remove seconds at the end if (time1.substring(5).equals(":00")) { time1 = time1.substring(0, 5); } return(time1); } // func String getDate( String date1 ) { // take care of date // convert to dd.mm.yyyy (German Standard) // (from "yyyy-mm-dd") date1 = date1.substring(8, 10) + "." + date1.substring(5, 7) + "." + date1.substring(0, 4) ; return(date1); } // func // ==================================================================== // Inputs void keyPressed() { if (key == CODED) { // CODED ------------------------------------------------ EvluateCodedKeys(); } else { // not Coded -------------------------------------- EvluateNotCodedKeys(); } // else } // func // ============================================================== void EvluateCodedKeys () { if (keyCode == RIGHT) { // move month up intCurrentMonth++; // boundary if (intCurrentMonth>12) { intCurrentMonth=1; } // if Output(); } // else if (keyCode == LEFT) { // move month down intCurrentMonth--; // boundary if (intCurrentMonth<1) { intCurrentMonth=12; } // if Output(); } // else { // do nothing } // else } // func // ------------------------------------------------ void EvluateNotCodedKeys () { if (key == '+') { // // do nothing } else if (key == '-') { // // do nothing } else { // do nothing } // else } // func // ====================================================================