JSON: Use API and show JSON data on screen

edited February 2014 in Share Your Work

I wanted to share a small JSON sketch

tested with processing 2.1

no API-key required

first line must be

String baseURL = "http://www.ekd.de/reformation-und-musik/api366+1.php?";

the forum kills the url

  • you can't scroll down but just go from month to month (using cursor left and right)

    //
    String baseURL = "http://www.ekd.de/reformation-und-musik/api366+1.php?";
    int intCurrentMonth = 3; 
    PFont myFont;
    
    void setup() {
      size(1400, 700);
      myFont = createFont("Sans Serif", 18);   
      textFont(myFont);
      fill(0);
      Output();
      println ( "you can use cursor left and right (you can't scroll)" );
    } // func 
    
    void draw() {
      // nothing here - 
      // It's all in keyPressed() (or in setup())
    } // func 
    
    // ===================================================
    
    void Output() {
      background (201);  
      // THE MAIN METHOD GETS CALLED HERE
      // Ausgabe JSON
      getContentFromJSON();
      // Ausgabe aktueller Monat / help text 
      text ( "Month: " + trim(str(intCurrentMonth) + " (Cursor <>)"), width-170, 15);
      fill(255, 0, 0);
      text ( "Use Cursor <- and -> to change month (" + trim(str(intCurrentMonth)) + ")", 20, 15);
      fill(0);
    } // func 
    
    // =================================================
    
    void getContentFromJSON() {
    
      final int AbstandOben   = 40;  // oberer Rand
      final int AbstandItems  = 39;  // 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
    
      // with intCurrentMonth <= 12 the value of intCurrentMonth
      // is interpreted as a month 
      String request = baseURL + "d=" + trim(str(intCurrentMonth)); 
      //
      //
      JSONArray Data1;
    
      //println (request);
      //println ();
      Data1 = loadJSONArray (request);
      //
      yPos = AbstandOben;
    
      // loop through the array 
      for (int i = 0; i < Data1.size(); i++) {
    
        JSONObject entry = Data1.getJSONObject(i);
    
        if (entry == null) {
          println ("entry == null ###############################");
        } 
    
        // headline: title and number of concert 
        textSize (18);
        String titleData = entry.getString("titel");
        if (textWidth ( titleData ) > width - 140 ) {
          int pos=110;
          char test1 = titleData.substring (pos).charAt(0) ;
          // println (test1); 
          while ( " .,-!?".indexOf (test1) == -1) {
            pos--;
            test1 = titleData.substring (pos).charAt(0)  ;
          }
          pos++;
          String titleData1 = titleData.substring (0, pos ) ;
          String titleData2 = titleData.substring (pos, titleData.length() ) ;
          text ( titleData1, xPos, yPos );
          yPos = yPos+23;
          text ( titleData2, xPos, yPos );
        } 
        else {  
          text ( titleData, xPos, yPos );
        }
    
        // normaler Text
        textSize (14);
        yPos = yPos + AbstandZeilen; 
        text ( "(No. " 
          + entry.getString("nummer") 
          + " of 366+1)", xPos, yPos );
    
        // normaler Text
        textSize (14);
        yPos = yPos + AbstandZeilen ;
        text (entry.getString("beschreibung") + " in " + 
          entry.getString("ort"), xPos, yPos );
    
        yPos = yPos + AbstandZeilen;
        String landeskirche_url_Data = "" ;
        try {
          if ( entry.getString("landeskirche_url") != null ) { 
            landeskirche_url_Data = entry.getString("landeskirche_url");
          }
        }
        catch (Exception e) {
          landeskirche_url_Data = "unknown url" ;
        }
        //println (landeskirche_url_Data); 
        text (entry.getString("landeskirche")  + ", " + 
          landeskirche_url_Data + ", in " + 
          entry.getString("bundesland"), xPos, yPos );
    
        yPos = yPos + AbstandZeilen ;
        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
        //
        yPos += AbstandItems;
        //
      } // for
      //
    } // 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 
    
    // ==================================================================
    
Tagged:

Comments

Sign In or Register to comment.