JSONObject example not running?

(Sorry found this issue... it works on 2.1b1. reference points to correct for 2.x which i took to include 2.0b3 but that isn't the case.)

I guess i am doing something wrong. I am on processing 2.0b3 OSX 10.8.3. Been using processing for years BUT can't get the JSONObject example to cut and paste and run. Just says cannot find a class or type named "JSONObject". So i did some reading and dropped a json.jar file on the sketch. And then it fails with "The function setInt(String, Int) does not exist".

I'm missing something stupidn like not importing something....it;s been a good year since i last used processing so i probably forgot something simple.

Tagged:

Answers

  • edited October 2013

    here...

    this works 2.01

    you can use cursor left and right

    it used to crash in month 2 (one item is null there) but I catch the error now.

    Also long headlines are splitted into two lines now

    The main point is that JSON is now built in into processing, so you don't need the lib anymore. But the usage seems to be very different from the old usage in the lib. Therefore I had to use JSONarray instead of JSONobject and make minor changes.

    I hope this helps.

    Greetings, Chrisir

  • new version which catches the error

    //
    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" );
    } // func 
    
    void draw() {
      // nothing here - 
      // It's all in keyPressed()
    } // func 
    
    // ===================================================
    
    void Output() {
      background (201);  
      // THE MAIN METHOD GETS CALLED HERE
      // Ausgabe JSON
      getContentFromJSON();
      // Ausgabe aktueller Monat 
      text ( "Month: " + trim(str(intCurrentMonth) + " (Cursor <>)"), width-170, 15);
    } // 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 
    
    // ====================================================================
    
  • Thanks for this. I think it was just confusing regards to libraries needed. I no longer include the library but i had to make the calls like this

    processing.data.JSONObject firstobj = theArray.getJSONObject(i);

    otherwise it just doesn't work.

  • Answer ✓

    great news!

    glad to hear...

Sign In or Register to comment.