4

I have an HTML table that is formatted like this:

<table>
    <tbody>
        <tr>
            <td>Book1</td>
            <td>Group1</td>
            <td>Code1</td>
            <td>Lesson1</td>
            <td>Day1</td>
            <td>Day2</td>
            <td>Day3</td>
        </tr>
        <tr>
            <td>Book2</td>
            <td>Group2</td>
            <td>Code2</td>
            <td>Lesson2</td>
            <td>Day1</td>
            <td>Day2</td>
            <td>Day3</td>
        </tr>
    </tbody>
</table>

I would like to parse this HTML with Jsoup, and output a JSON string formatted like this:

{
   "Book1": {
      "Group": "Group1",
      "Code": "Code1",
      "Lesson": "Lesson1",
      "Day1": "Day1",
      "Day2": "Day2",
      "Day3": "Day3"
   },
   "Book2": {
      "Group": "Group2",
      "Code": "Code2",
      "Lesson": "Lesson2",
      "Day1": "Day1",
      "Day2": "Day2",
      "Day3": "Day3"
   }
}

I tried this code:

public String TableToJson(String source) throws JSONException {
    Document doc = Jsoup.parse(source);
    JSONObject jsonObject = new JSONObject();
    JSONArray list = new JSONArray();
    for (Element table : doc.select("table")) {
        for (Element row : table.select("tr")) {
            Elements tds = row.select("td");
            String Name = tds.get(0).text();
            String Group = tds.get(1).text();
            String Code = tds.get(2).text();

            jsonObject.put("Name", Name); 
            jsonObject.put("Group", Group);
            jsonObject.put("Code", Code);
            list.put(jsonObject);
        }
    }
    return list.toString();
}

But it returned the wrong result:

[
    {
        "Name": "Book1",
        "Group": "Group1",
        "Code": "Code1"
    },
    {
        "Name": "Book1",
        "Group": "Group1",
        "Code": "Code1"
    }
]

I can't change table code because it's on another server.

How can I get the desired result from the input using Jsoup in Java?

3
  • 1
    have you tried anything? getting an error? Commented Feb 24, 2017 at 20:19
  • 1
    @Coder i edit question Commented Feb 25, 2017 at 6:49
  • 1
    You are using the same JSONObject. Commented Feb 25, 2017 at 7:56

1 Answer 1

6

The problem with your code is you are trying to make use of same jsonObject and you are also using JsonArray which you don't need. You need an object containing objects but not object containing array of objects

public String TableToJson(String source) throws JSONException {   
     Document doc = Jsoup.parse(source);
        JSONObject jsonParentObject = new JSONObject();
        //JSONArray list = new JSONArray();
        for (Element table : doc.select("table")) {
            for (Element row : table.select("tr")) {
                JSONObject jsonObject = new JSONObject();
                Elements tds = row.select("td");
                String Name = tds.get(0).text();
                String Group = tds.get(1).text();
                String Code = tds.get(2).text();
                String Lesson = tds.get(3).text();
                String Day1 = tds.get(4).text();
                String Day2 = tds.get(5).text();
                String Day3= tds.get(6).text();        
                jsonObject.put("Group", Group);
                jsonObject.put("Code", Code);
                jsonObject.put("Lesson", Lesson);
                jsonObject.put("Day1", Day1);
                jsonObject.put("Day2", Day2);
                jsonObject.put("Day3", Day3);
                jsonParentObject.put(Name,jsonObject);
             }
        }
    return jsonParentObject.toString();
}

Let me know if you need clarification!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.