Accessing nested values in a table

here

I know it sucks…

I think it’s floats inside an JSONArray inside a JSONObject inside a JSONObject…

// https://discourse.processing.org/t/accessing-nested-values-in-a-table/9761/2

// data (The \" means " in String)
String data =
  "{"+
  " \"0\":{"+
  "     \"0\":[40.65, 20.30, 120.92, 86.12, 99.33],"+
  "     \"1\":[10.35, 90.10, 10.52,  1.12,  59.3],"+
  "     \"2\":[10.65, 62.10, 12.12,  96.23, 19.3],"+
  " }"+
  "}";

// class with tools 
Tools tools = new Tools(); 

// the table
Table tableValues=null; 

void setup() {
  size(660, 660);
  // parse the above String 
  JSONObject json = parseJSONObject(data);

  // Success? 
  if (json == null) {
    // Fail 
    println("JSONObject could not be parsed +++++++++++++++++++++++++");
    exit(); 
    return;
  } else {
    // Success 

    JSONObject myJSONObject = json.getJSONObject("0"); 
    println(myJSONObject);

    // setting up table (column names / headlines)
    tableValues=tools.setUpTable(myJSONObject); 

    // adding data (must match headlines)
    tableValues=tools.addJSONArrayToTable(tableValues, myJSONObject, "0");
    tableValues=tools.addJSONArrayToTable(tableValues, myJSONObject, "1");
    tableValues=tools.addJSONArrayToTable(tableValues, myJSONObject, "2");
  }//else
}// setup

void draw() {
  // 
  background(0); 
  tools.showTable(tableValues, 
    22, 22);

  fill(tools.WHITE); 
  text("use mouse over to show full text", 
    22, height/2-110);
}// draw 

//===============================================================================
// Tools collection 

class Tools {

  // class not like a car class Car for an object but a collection of tools. 

  final color RED   = color(255, 0, 0); 
  final color GREEN = color(0, 255, 0); 
  final color BLUE  = color(0, 0, 255); 

  final color WHITE = color(255); 
  final color BLACK = color(0); 
  final color GRAY  = color(255/2); 

  boolean cursorSignShowFlag=false; 

  // ----------------------------------------------------------------

  Table setUpTable(JSONObject myJSONObject_) {
    // expects a JSONObject with a JSONArray
    // make a new table from scratch 
    Table newT = new Table();
    JSONArray values = myJSONObject_.getJSONArray( "0" );    // https://www.processing.org/reference/JSONObject_getJSONArray_.html
    String[] headers1 = new String[values.size()]; 
    for (int i = 0; i < values.size(); i++) {
      headers1[i] = str(i);
    }//for
    newT = newTable(headers1);
    return newT;
  }

  Table addJSONArrayToTable ( Table table1, JSONObject myJSONObject_, String line_ ) {
    // expects a table of same size 
    // expects a JSONObject with a JSONArray
    // expects a line number for the JSONArray  
    JSONArray values = myJSONObject_.getJSONArray( line_ );    // https://www.processing.org/reference/JSONObject_getJSONArray_.html
    String[] values1 = new String[values.size()]; 
    for (int i = 0; i < values.size(); i++) {
      values1[i] = str(values.getFloat(i));
    }//for
    table1 = tableAddData ( table1, values1 );
    return table1;
  }

  // ----------------------------------------------------------------

  void showTable(Table tableBtn, 
    int x, int y) { 

    int factorX=78; // column width 

    // rect
    stroke(WHITE);
    noFill();
    rect( x, y, 
      tableBtn.getColumnCount()*78-6, (tableBtn.getRowCount()+1) * 22 + 10 );

    // headline 
    showTableHeadline(tableBtn, x+6, y+19, factorX);

    // horizontal line 
    stroke(WHITE);
    line( x+2, y+5+19, 
      6+x+(tableBtn.getColumnCount())*factorX-13, y+5+19);

    // grid 
    // loop over rows (y)
    for (int i=0; i<tableBtn.getRowCount(); i++) {

      // current data row
      TableRow row = tableBtn.getRow(i);

      // loop over columns in that row (i2 is for x) 
      for (int i2=0; i2<tableBtn.getColumnCount(); i2++) {

        fill(WHITE);
        text(row.getString(i2), 
          i2*factorX+x+6, 25+ i * 22 +y+8, 
          factorX-8, 15);

        if (mouseInside(i2*factorX+x+6, 25+ i * 22 +y+8, 
          factorX-8, 15)) {
          text (row.getString(i2), 
            20, height-22);
        }//if

        // vertical line 
        line( i2*factorX+x, +y, 
          i2*factorX+x, tableBtn.getRowCount() * 22 + y + 31);
      }//for
    }//for
  }// method 

  boolean mouseInside( float x_, float y_, 
    float w_, float h_) {
    return mouseX>x_ &&
      mouseX<x_+w_ &&
      mouseY>y_ &&
      mouseY<y_+h_;
  }

  void showTableHeadline(Table tableBtn, 
    int x, int y, 
    int factorX) { 
    // headline for table 
    TableRow row0 = tableBtn.getRow(0);
    for (int i=0; i<tableBtn.getColumnCount(); i++) {
      // headline 
      fill(GREEN);
      text(row0.getColumnTitle(i), 
        i*factorX+x, y-2);
    }
  }//method 

  // ---
  // make table 

  Table newTable (String... listColumnNames) { 

    Table newT = new Table();

    // make columns
    for (String s1 : listColumnNames) {
      newT.addColumn(s1);
    }

    return newT;
  }//method

  Table tableAddData( Table table1, String... data1  ) {

    // add rows with data 
    TableRow newRow = table1.addRow();

    // add rows with data 
    int i=0; 
    for (String s1 : data1) {
      newRow.setString(newRow.getColumnTitle(i), s1);
      i++;
    }
    return table1;
  }//method

  // --- 

  void printlnTable(Table tableBtn) { 

    // rect
    stroke(WHITE);
    noFill();

    println("------------------------------------");

    // headline 
    printlnTableHeadline(tableBtn);

    // grid 
    // loop over rows (y)
    for (int i=0; i<tableBtn.getRowCount(); i++) {

      // current data row
      TableRow row = tableBtn.getRow(i);

      // loop over columns in that row (i2 is for x)
      String s1="";
      for (int i2=0; i2<tableBtn.getColumnCount(); i2++) {
        s1+="   "+row.getString(i2);
        //
      }//for
      println(s1);
    }//for
    println("===========================================");
    //
  } // method 

  void printlnTableHeadline(Table tableBtn ) { 
    // headline for table 
    TableRow row0 = tableBtn.getRow(0);
    for (int i=0; i<tableBtn.getColumnCount(); i++) {
      // headline 
      print("   "+row0.getColumnTitle(i));
    }
    println("");
  }//method 

  // ------------------------------------------------------------------

  String cursorSign() {
    // blinking cursor sign | for Input Box 
    if (frameCount % 13 == 0)
      cursorSignShowFlag= ! cursorSignShowFlag;
    if (cursorSignShowFlag)
      return"|";
    else return"";
  }//method

  //
}//class
//
3 Likes