我有一个HTML表,格式如下:

<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>

我想用jsoup解析这个html,并输出如下格式的json字符串:
{
   "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"
   }
}

我试过这个密码:
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();
}

但它返回了错误的结果:
[
    {
        "Name": "Book1",
        "Group": "Group1",
        "Code": "Code1"
    },
    {
        "Name": "Book1",
        "Group": "Group1",
        "Code": "Code1"
    }
]

我无法更改表代码,因为它在另一台服务器上。
如何使用java中的jsoup从输入中获得所需的结果?

最佳答案

您的代码的问题是您试图使用相同的jsonObject,而且您也在使用不需要的JsonArray。您需要一个包含objects的对象,而不是包含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();
}

如果你需要澄清,请告诉我!

09-25 15:26