Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract data from a HTML table and convert to JSON

Tags:

java

json

arrays

I have a HTML table that I want to parse and convert to JSON.

<table cellspacing="0" style="height: 24px;">
 <tr class="tr-hover">
  <th rowspan="15" scope="row">Network</th>
  <td class="ttl"><a href="network-bands.php3">Technology</a></td>
  <td class="nfo"><a href="#" class="link-network-detail collapse">GSM</a></td>
 </tr>
 <tr class="tr-toggle">
  <td class="ttl"><a href="network-bands.php3">2G bands</a></td>
  <td class="nfo">GSM 900 / 1800 - SIM 1 & SIM 2</td>
 </tr>  
 <tr class="tr-toggle">
  <td class="ttl"><a href="glossary.php3?term=gprs">GPRS</a></td>
  <td class="nfo">Class 12</td>
 </tr>  
 <tr class="tr-toggle">
  <td class="ttl"><a href="glossary.php3?term=edge">EDGE</a></td>
  <td class="nfo">Yes</td>
 </tr>
</table>

In the above table

<th rowspan="15" scope="row">Network</th> 

JSON array name should be "Network".

<td class="ttl"><a href="network-bands.php3">Technology</a></td>

Technology is a subheading of Network, so it must be a JSON element inside the JSON array. The values coming inside Technology array should be the values from

<td class="nfo"><a href="#" class="link-network-detail collapse">GSM</a></td>

I hope my question is clear. How can i do that?

like image 252
Miller Avatar asked Dec 04 '25 10:12

Miller


1 Answers

Here is an answer using Jsoup and JSON as dependencies:

final String HTML = "<table cellspacing=\"0\" style=\"height: 24px;\">\r\n<tr class=\"tr-hover\">\r\n<th rowspan=\"15\" scope=\"row\">Network</th>\r\n<td class=\"ttl\"><a href=\"network-bands.php3\">Technology</a></td>\r\n<td class=\"nfo\"><a href=\"#\" class=\"link-network-detail collapse\">GSM</a></td>\r\n</tr>\r\n<tr class=\"tr-toggle\">\r\n<td class=\"ttl\"><a href=\"network-bands.php3\">2G bands</a></td>\r\n<td class=\"nfo\">GSM 900 / 1800 - SIM 1 & SIM 2</td>\r\n</tr>   \r\n<tr class=\"tr-toggle\">\r\n<td class=\"ttl\"><a href=\"glossary.php3?term=gprs\">GPRS</a></td>\r\n<td class=\"nfo\">Class 12</td>\r\n</tr>   \r\n<tr class=\"tr-toggle\">\r\n<td class=\"ttl\"><a href=\"glossary.php3?term=edge\">EDGE</a></td>\r\n<td class=\"nfo\">Yes</td>\r\n</tr>\r\n</table>";
Document document = Jsoup.parse(HTML);
Element table = document.select("table").first();
String arrayName = table.select("th").first().text();
JSONObject jsonObj = new JSONObject();
JSONArray jsonArr = new JSONArray();
Elements ttls = table.getElementsByClass("ttl");
Elements nfos = table.getElementsByClass("nfo");
JSONObject jo = new JSONObject();
for (int i = 0, l = ttls.size(); i < l; i++) {
    String key = ttls.get(i).text();
    String value = nfos.get(i).text();
    jo.put(key, value);
}
jsonArr.put(jo);
jsonObj.put(arrayName, jsonArr);
System.out.println(jsonObj.toString());

Output (formatted):

{
    "Network": [
        {
            "2G bands": "GSM 900 / 1800 - SIM 1 & SIM 2",
            "Technology": "GSM",
            "GPRS": "Class 12",
            "EDGE": "Yes"
        }
    ]
}
like image 164
Jared Rummler Avatar answered Dec 06 '25 22:12

Jared Rummler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!