title: Java loop on multiple HTML table rows
body: <p>Please note that I am no Java expert, I would however appreciate any help and guidance where possible.</p>
  
  <p>I have a piece of Java code that reads the contents of a files and parses it to html using JSOUP. I then need to convert a table into JSON. The first row converts successfully, but none of the following rows are getting converted.</p>
  
  <p>I am sure that something simple is missing that I am over looking, so would appreciate any guidance / assistance.</p>
  
  <p>My program below:</p>
  
  <pre><code>package html_to_json_parser;
  
  import java.io.BufferedReader;
  import java.io.FileInputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import org.json.JSONObject;
  import org.jsoup.Jsoup;
  import org.jsoup.nodes.Document;
  import org.jsoup.nodes.Element;
  import org.jsoup.select.Elements;
  
  
  public class HTML_to_JSON_parser {
  
  
  public static void main(String[] args) throws IOException {
  
      InputStream is = new FileInputStream("skills_table.html"); 
      BufferedReader buf = new BufferedReader(new InputStreamReader(is)); 
      String line = buf.readLine(); 
      StringBuilder sb = new StringBuilder(); 
  
      while(line != null){
          sb.append(line).append("\n");
          line = buf.readLine(); 
      }
  
      String htmlFile = sb.toString(); 
  
      Document doc = Jsoup.parse(htmlFile);
      JSONObject jsonObject = new JSONObject();
      JSONObject jsonParentObject = new JSONObject();
      Element table = doc.select("table").first();
      Elements row = table.getElementsByTag("tr");        
  
          for (int j = 0, k = row.size(); j &lt; k; j++) {
  
              Elements cols = row.select("td");
              String skill = cols.get(0).text();
              String beginner = cols.get(1).text();
              String intermediate = cols.get(2).text();
              String advanced = cols.get(3).text();
              jsonObject.put("Beginner", beginner);
              jsonObject.put("Intermediate", intermediate);
              jsonObject.put("Advanced", advanced);
              jsonParentObject.put(skill,jsonObject);
           }
  
      System.out.println(jsonParentObject.toString());
  
      }
  
  }
  </code></pre>
  
  <p>HTML table that needs to be converted to JSON:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;table&gt;
    &lt;thead&gt;
      &lt;tr&gt;
        &lt;th&gt;Skill&lt;/th&gt;
        &lt;th&gt;Beginner&lt;/th&gt;
        &lt;th&gt;Intermediate&lt;/th&gt;
        &lt;th&gt;Advanced&lt;/th&gt;
      &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
      &lt;tr&gt;
        &lt;td&gt;Reading&lt;/td&gt;
        &lt;td&gt;✔&lt;/td&gt;
        &lt;td&gt;✔&lt;/td&gt;
        &lt;td&gt;✔&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td&gt;Speaking&lt;/td&gt;
        &lt;td&gt;◯&lt;/td&gt;
        &lt;td&gt;✔&lt;/td&gt;
        &lt;td&gt;✔&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td&gt;Writing&lt;/td&gt;
        &lt;td&gt;◯&lt;/td&gt;
        &lt;td&gt;◯&lt;/td&gt;
        &lt;td&gt;✔&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td&gt;Interacting&lt;/td&gt;
        &lt;td&gt;●&lt;/td&gt;
        &lt;td&gt;●&lt;/td&gt;
        &lt;td&gt;● ●&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;
  &lt;/table&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>Require JSON output:</p>
  
  <pre><code>[
      {
          "skill": "reading",
          "beginner": "✔",
          "intermediate": "✔",
          "advanced": "✔"
      },
      {
          "skill": "writing",
          "beginner": "◯",
          "intermediate": "✔",
          "advanced": "✔"
      },
      {
          "skill": "speaking",
          "beginner": "◯",
          "intermediate": "◯",
          "advanced": "✔"
      },
      {
          "skill": "interacting",
          "beginner": "●",
          "intermediate": "●",
          "advanced": "● ●"
      }
  ]
  </code></pre>
  
  <p>Your guidance and assistance is greatly appreciated.</p>