当前位置: 首页 > 知识库问答 >
问题:

将HTML表转换为JSON(Python)

公孙锋
2023-03-14

我有很多HTML表,我正在尝试将其转换为json格式,但我的代码只适用于第一个水平表(第一个图像),而不是第二个垂直表(第二个图像)...

我在这里附上了我的代码和示例表

到目前为止我尝试的代码

html_data=Path("Table2.html").read_text()
table_data = [[cell.text for cell in row("td")]
                         for row in BeautifulSoup(html_data,features="lxml")("tr")]
json_data=[]
for list1 in table_data:
    list1 = [i.replace('\n', '') for i in list1]
    dict1 = dict(itertools.zip_longest(*[iter(list1)] * 2, fillvalue=""))
    json_data.append(dict1)
print(json_data)

以上HTML表的输出:

[{'Address': '41 B Market street'}, {'City': 'Gujarat'}, {'Postal/Zip Code': '123456'}, {'Product Details': ''}, {'Pallet Dimension': '10" x 10" x 10"'}, {'Total Weight': '1375 LBS'}]

[{'Pickup Location': 'Description', '': ''}, {'Some Address': 'Rubics cube', '': ''}, {}, {'PLTS': 'total weight', 'L': 'W', 'H': ''}, {'1': '20', '40': ''}, {'2': '60', '40': ''}]

表2的HTML代码

<table>
<tbody>
<tr style="height:15.0pt">
<td colspan="2" style="width:130.9pt; border-top:solid windowtext 1.0pt; border-left:none; border-bottom:solid windowtext 1.0pt; border-right:solid black 1.0pt; padding:0in 5.4pt 0in 5.4pt; height:15.0pt" width="175">
<p class="MsoNormal" style="text-align:center" align="center"><b><span style="font-size:10.0pt; font-family:&quot;Arial&quot;,sans-serif; color:black">Pickup Location</span></b></p>
</td>
<td colspan="3" style="width:130.1pt; border-top:solid windowtext 1.0pt; border-left:none; border-bottom:solid windowtext 1.0pt; border-right:solid black 1.0pt; padding:0in 5.4pt 0in 5.4pt; height:15.0pt" width="173">
<p class="MsoNormal" style="text-align:center" align="center"><b><span style="font-size:10.0pt; font-family:&quot;Arial&quot;,sans-serif; color:black">Description</span></b></p>
</td>
<td style="width:1.5pt; padding:0in 0in 0in 0in; height:15.0pt" width="2">
<p class="MsoNormal"></p>
</td>
<td style="width:.3pt; padding:0in 0in 0in 0in; height:15.0pt" width="0"></td>
</tr>
<tr style="height:13.15pt">
<td colspan="2" rowspan="2" style="width:130.9pt; border-top:none; border-left:none; border-bottom:solid black 1.0pt; border-right:solid windowtext 1.0pt; padding:0in 5.4pt 0in 5.4pt; height:13.15pt" width="175">
<p class="MsoNormal" style="text-align:center" align="center"><b><span style="font-size:10.0pt; font-family:&quot;Arial&quot;,sans-serif; color:black">Some Address</span></b></p>
</td>
<td colspan="3" rowspan="2" style="width:130.1pt; border-top:none; border-left:none; border-bottom:solid black 1.0pt; border-right:solid black 1.0pt; padding:0in 5.4pt 0in 5.4pt; height:13.15pt" width="173">
<p class="MsoNormal" style="text-align:center" align="center"><b><span style="font-size:10.0pt; font-family:&quot;Arial&quot;,sans-serif; color:black">Rubics cube</span></b></p>
</td><td style="width:1.5pt; padding:0in 0in 0in 0in; height:13.15pt" width="2">
<p class="MsoNormal"></p>
</td>
<td style="width:.3pt; padding:0in 0in 0in 0in; height:13.15pt" width="0"></td>
</tr>
<tr style="height:15.75pt">
</tr>
<tr style="height:.3in">
<td style="width:42.15pt; border-top:none; border-left:none; border-bottom:solid windowtext 1.0pt; border-right:solid windowtext 1.0pt; background:#D9E1F2; padding:0in 5.4pt 0in 5.4pt; height:.3in" width="56" valign="bottom" nowrap="nowrap">
<p class="MsoNormal" style="text-align:center" align="center"><b><span style="font-size:10.0pt; font-family:&quot;Arial&quot;,sans-serif; color:black">PLTS</span></b><b><span style="font-size:10.0pt; font-family:&quot;Arial&quot;,sans-serif"></span></b></p>
</td>
<td style="width:88.75pt; border-top:none; border-left:none; border-bottom:solid windowtext 1.0pt; border-right:solid windowtext 1.0pt; background:#D9E1F2; padding:0in 5.4pt 0in 5.4pt; height:.3in" width="118" valign="bottom" nowrap="nowrap">
<p class="MsoNormal" style="text-align:center" align="center"><b><span style="font-size:10.0pt; font-family:&quot;Arial&quot;,sans-serif; color:black">total weight</span></b><b><span style="font-size:10.0pt; font-family:&quot;Arial&quot;,sans-serif"></span></b></p>
</td>
<td style="width:20.15pt; border-top:none; border-left:none; border-bottom:solid windowtext 1.0pt; border-right:solid windowtext 1.0pt; background:#D9E1F2; padding:0in 5.4pt 0in 5.4pt; height:.3in" width="27" valign="bottom" nowrap="nowrap">
<p class="MsoNormal" style="text-align:center" align="center"><b><span style="font-size:10.0pt; font-family:&quot;Arial&quot;,sans-serif; color:black">L</span></b><b><span style="font-size:10.0pt; font-family:&quot;Arial&quot;,sans-serif"></span></b></p>
</td>
<td style="width:20.15pt; border-top:none; border-left:none; border-bottom:solid windowtext 1.0pt; border-right:solid windowtext 1.0pt; background:#D9E1F2; padding:0in 5.4pt 0in 5.4pt; height:.3in" width="27" valign="bottom" nowrap="nowrap">
<p class="MsoNormal" style="text-align:center" align="center"><b><span style="font-size:10.0pt; font-family:&quot;Arial&quot;,sans-serif; color:black">W</span></b><b><span style="font-size:10.0pt; font-family:&quot;Arial&quot;,sans-serif"></span></b></p>
</td>
<td style="width:17.0pt; border-top:none; border-left:none; border-bottom:solid windowtext 1.0pt; border-right:solid windowtext 1.0pt; background:#D9E1F2; padding:0in 5.4pt 0in 5.4pt; height:.3in" width="23" valign="bottom" nowrap="nowrap">
<p class="MsoNormal" style="text-align:center" align="center"><b><span style="font-size:10.0pt; font-family:&quot;Arial&quot;,sans-serif; color:black">H</span></b><b><span style="font-size:10.0pt; font-family:&quot;Arial&quot;,sans-serif"></span></b></p>
</td>
</tr>
<tr style="height:13.9pt">
<td style="width:42.15pt; border-top:none; border-left:none; border-bottom:solid windowtext 1.0pt; border-right:solid windowtext 1.0pt; padding:0in 5.4pt 0in 5.4pt; height:13.9pt" width="56" valign="bottom" nowrap="nowrap">
<p class="MsoNormal" style="text-align:center" align="center"><span style="font-size:12.0pt; font-family:&quot;Arial&quot;,sans-serif; color:black">1</span></p>
</td>
<td style="width:88.75pt; border-top:none; border-left:none; border-bottom:solid windowtext 1.0pt; border-right:solid windowtext 1.0pt; padding:0in 5.4pt 0in 5.4pt; height:13.9pt" width="118" valign="bottom" nowrap="nowrap">
<p class="MsoNormal" style="text-align:center" align="center"><span style="font-size:12.0pt; font-family:&quot;Arial&quot;,sans-serif">20</span></p>
</td>
<td style="width:20.15pt; border-top:none; border-left:none; border-bottom:solid windowtext 1.0pt; border-right:solid windowtext 1.0pt; padding:0in 5.4pt 0in 5.4pt; height:13.9pt" width="27" valign="bottom" nowrap="nowrap">
<p class="MsoNormal" style="text-align:center" align="center"><span style="font-size:12.0pt; font-family:&quot;Arial&quot;,sans-serif">40</span></p>
</td>
<td style="width:20.15pt; border-top:none; border-left:none; border-bottom:solid windowtext 1.0pt; border-right:solid windowtext 1.0pt; padding:0in 5.4pt 0in 5.4pt; height:13.9pt" width="27" valign="bottom" nowrap="nowrap">
<p class="MsoNormal" style="text-align:center" align="center"><span style="font-size:12.0pt; font-family:&quot;Arial&quot;,sans-serif">40</span></p>
</td>
<td style="width:17.0pt; border-top:none; border-left:none; border-bottom:solid windowtext 1.0pt; border-right:solid windowtext 1.0pt; padding:0in 5.4pt 0in 5.4pt; height:13.9pt" width="23" valign="bottom" nowrap="nowrap">
<p class="MsoNormal" style="text-align:center" align="center"><span style="font-size:12.0pt; font-family:&quot;Arial&quot;,sans-serif">40</span></p>
</td>
<td style="width:.3pt; padding:0in 0in 0in 0in; height:13.9pt" width="0"></td>
</tr>
<tr style="height:13.15pt">
<td style="width:42.15pt; border-top:none; border-left:none; border-bottom:solid windowtext 1.0pt; border-right:solid windowtext 1.0pt; padding:0in 5.4pt 0in 5.4pt; height:13.15pt" width="56" valign="bottom" nowrap="nowrap">
<p class="MsoNormal" style="text-align:center" align="center"><span style="font-size:12.0pt; font-family:&quot;Arial&quot;,sans-serif; color:black">2</span></p>
</td>
<td style="width:88.75pt; border-top:none; border-left:none; border-bottom:solid windowtext 1.0pt; border-right:solid windowtext 1.0pt; padding:0in 5.4pt 0in 5.4pt; height:13.15pt" width="118" valign="bottom" nowrap="nowrap">
<p class="MsoNormal" style="text-align:center" align="center"><span style="font-size:12.0pt; font-family:&quot;Arial&quot;,sans-serif">60</span></p>
</td>
<td style="width:20.15pt; border-top:none; border-left:none; border-bottom:solid windowtext 1.0pt; border-right:solid windowtext 1.0pt; padding:0in 5.4pt 0in 5.4pt; height:13.15pt" width="27" valign="bottom" nowrap="nowrap">
<p class="MsoNormal" style="text-align:center" align="center"><span style="font-size:12.0pt; font-family:&quot;Arial&quot;,sans-serif">40</span></p>
</td>
<td style="width:20.15pt; border-top:none; border-left:none; border-bottom:solid windowtext 1.0pt; border-right:solid windowtext 1.0pt; padding:0in 5.4pt 0in 5.4pt; height:13.15pt" width="27" valign="bottom" nowrap="nowrap">
<p class="MsoNormal" style="text-align:center" align="center"><span style="font-size:12.0pt; font-family:&quot;Arial&quot;,sans-serif">40</span></p>
</td>
<td style="width:17.0pt; border-top:none; border-left:none; border-bottom:solid windowtext 1.0pt; border-right:solid windowtext 1.0pt; padding:0in 5.4pt 0in 5.4pt; height:13.15pt" width="23" valign="bottom" nowrap="nowrap">
<p class="MsoNormal" style="text-align:center" align="center"><span style="font-size:12.0pt; font-family:&quot;Arial&quot;,sans-serif">40</span></p>
</td>
</tr>
</tbody>
</table>

null

如果表是水平表(表1),那么旧的输出就足够了

[{'Address': '41 B Market street'}, {'City': 'Gujarat'}, {'Postal/Zip Code': '123456'}, {'Product Details': ''}, {'Pallet Dimension': '10" x 10" x 10"'}, {'Total Weight': '1375 LBS'}]

如果表是一个垂直表(表2),那么输出应该如下所示:

[{'Pickup address': 'some address'}, {'Description': 'Rubicks cube'}, {'PLTS': ['1','2']}, {'Total weight': ['20','60']}, {'L':['40','40']}, {'W':['40','40']},{'H':['40','40']}]

我试过修改代码,但对我没有任何建议???

共有1个答案

荆鸿畅
2023-03-14

我试图使这个解决方案尽可能通用,同时也满足所提出的具体案例。这只适用于第二种情况,因为没有th元素或仅在头元素上有特定的类,所以无法通过编程确定头是垂直的还是水平的。

from bs4 import BeautifulSoup
from pathlib import Path
import itertools

html_data = Path("table.html").read_text()
table_data = [[td.text.strip() for td in tr("td") if td.text.strip()]
              for tr in BeautifulSoup(html_data, features="lxml")("tr")]

out = [dict([(t, rest if len(rest) > 1 else rest[0]) for t, *rest in zip(*g)]) for k, g in
       itertools.groupby(table_data, key=bool) if k]
print(out)

输出:

[{'Pickup Location': 'Some Address', 'Description': 'Rubics cube'}, {'PLTS': ['1', '2'], 'total weight': ['20', '60'], 'L': ['40', '40'], 'W': ['40', '40'], 'H': ['40', '40']}]

table.html

<table>
    <tbody>
    <tr style="height:15.0pt">
        <td colspan="2"
            style="width:130.9pt; border-top:solid windowtext 1.0pt; border-left:none; border-bottom:solid windowtext 1.0pt; border-right:solid black 1.0pt; padding:0in 5.4pt 0in 5.4pt; height:15.0pt"
            width="175">
            <p class="MsoNormal" style="text-align:center" align="center"><b><span
                    style="font-size:10.0pt; font-family:&quot;Arial&quot;,sans-serif; color:black">Pickup Location</span></b>
            </p>
        </td>
        <td colspan="3"
            style="width:130.1pt; border-top:solid windowtext 1.0pt; border-left:none; border-bottom:solid windowtext 1.0pt; border-right:solid black 1.0pt; padding:0in 5.4pt 0in 5.4pt; height:15.0pt"
            width="173">
            <p class="MsoNormal" style="text-align:center" align="center"><b><span
                    style="font-size:10.0pt; font-family:&quot;Arial&quot;,sans-serif; color:black">Description</span></b>
            </p>
        </td>
        <td style="width:1.5pt; padding:0in 0in 0in 0in; height:15.0pt" width="2">
            <p class="MsoNormal"></p>
        </td>
        <td style="width:.3pt; padding:0in 0in 0in 0in; height:15.0pt" width="0"></td>
    </tr>
    <tr style="height:13.15pt">
        <td colspan="2" rowspan="2"
            style="width:130.9pt; border-top:none; border-left:none; border-bottom:solid black 1.0pt; border-right:solid windowtext 1.0pt; padding:0in 5.4pt 0in 5.4pt; height:13.15pt"
            width="175">
            <p class="MsoNormal" style="text-align:center" align="center"><b><span
                    style="font-size:10.0pt; font-family:&quot;Arial&quot;,sans-serif; color:black">Some Address</span></b>
            </p>
        </td>
        <td colspan="3" rowspan="2"
            style="width:130.1pt; border-top:none; border-left:none; border-bottom:solid black 1.0pt; border-right:solid black 1.0pt; padding:0in 5.4pt 0in 5.4pt; height:13.15pt"
            width="173">
            <p class="MsoNormal" style="text-align:center" align="center"><b><span
                    style="font-size:10.0pt; font-family:&quot;Arial&quot;,sans-serif; color:black">Rubics cube</span></b>
            </p>
        </td>
        <td style="width:1.5pt; padding:0in 0in 0in 0in; height:13.15pt" width="2">
            <p class="MsoNormal"></p>
        </td>
        <td style="width:.3pt; padding:0in 0in 0in 0in; height:13.15pt" width="0"></td>
    </tr>
    <tr style="height:15.75pt">
    </tr>
    <tr style="height:.3in">
        <td style="width:42.15pt; border-top:none; border-left:none; border-bottom:solid windowtext 1.0pt; border-right:solid windowtext 1.0pt; background:#D9E1F2; padding:0in 5.4pt 0in 5.4pt; height:.3in"
            width="56" valign="bottom" nowrap="nowrap">
            <p class="MsoNormal" style="text-align:center" align="center"><b><span
                    style="font-size:10.0pt; font-family:&quot;Arial&quot;,sans-serif; color:black">PLTS</span></b><b><span
                    style="font-size:10.0pt; font-family:&quot;Arial&quot;,sans-serif"></span></b></p>
        </td>
        <td style="width:88.75pt; border-top:none; border-left:none; border-bottom:solid windowtext 1.0pt; border-right:solid windowtext 1.0pt; background:#D9E1F2; padding:0in 5.4pt 0in 5.4pt; height:.3in"
            width="118" valign="bottom" nowrap="nowrap">
            <p class="MsoNormal" style="text-align:center" align="center"><b><span
                    style="font-size:10.0pt; font-family:&quot;Arial&quot;,sans-serif; color:black">total weight</span></b><b><span
                    style="font-size:10.0pt; font-family:&quot;Arial&quot;,sans-serif"></span></b></p>
        </td>
        <td style="width:20.15pt; border-top:none; border-left:none; border-bottom:solid windowtext 1.0pt; border-right:solid windowtext 1.0pt; background:#D9E1F2; padding:0in 5.4pt 0in 5.4pt; height:.3in"
            width="27" valign="bottom" nowrap="nowrap">
            <p class="MsoNormal" style="text-align:center" align="center"><b><span
                    style="font-size:10.0pt; font-family:&quot;Arial&quot;,sans-serif; color:black">L</span></b><b><span
                    style="font-size:10.0pt; font-family:&quot;Arial&quot;,sans-serif"></span></b></p>
        </td>
        <td style="width:20.15pt; border-top:none; border-left:none; border-bottom:solid windowtext 1.0pt; border-right:solid windowtext 1.0pt; background:#D9E1F2; padding:0in 5.4pt 0in 5.4pt; height:.3in"
            width="27" valign="bottom" nowrap="nowrap">
            <p class="MsoNormal" style="text-align:center" align="center"><b><span
                    style="font-size:10.0pt; font-family:&quot;Arial&quot;,sans-serif; color:black">W</span></b><b><span
                    style="font-size:10.0pt; font-family:&quot;Arial&quot;,sans-serif"></span></b></p>
        </td>
        <td style="width:17.0pt; border-top:none; border-left:none; border-bottom:solid windowtext 1.0pt; border-right:solid windowtext 1.0pt; background:#D9E1F2; padding:0in 5.4pt 0in 5.4pt; height:.3in"
            width="23" valign="bottom" nowrap="nowrap">
            <p class="MsoNormal" style="text-align:center" align="center"><b><span
                    style="font-size:10.0pt; font-family:&quot;Arial&quot;,sans-serif; color:black">H</span></b><b><span
                    style="font-size:10.0pt; font-family:&quot;Arial&quot;,sans-serif"></span></b></p>
        </td>
    </tr>
    <tr style="height:13.9pt">
        <td style="width:42.15pt; border-top:none; border-left:none; border-bottom:solid windowtext 1.0pt; border-right:solid windowtext 1.0pt; padding:0in 5.4pt 0in 5.4pt; height:13.9pt"
            width="56" valign="bottom" nowrap="nowrap">
            <p class="MsoNormal" style="text-align:center" align="center"><span
                    style="font-size:12.0pt; font-family:&quot;Arial&quot;,sans-serif; color:black">1</span></p>
        </td>
        <td style="width:88.75pt; border-top:none; border-left:none; border-bottom:solid windowtext 1.0pt; border-right:solid windowtext 1.0pt; padding:0in 5.4pt 0in 5.4pt; height:13.9pt"
            width="118" valign="bottom" nowrap="nowrap">
            <p class="MsoNormal" style="text-align:center" align="center"><span
                    style="font-size:12.0pt; font-family:&quot;Arial&quot;,sans-serif">20</span></p>
        </td>
        <td style="width:20.15pt; border-top:none; border-left:none; border-bottom:solid windowtext 1.0pt; border-right:solid windowtext 1.0pt; padding:0in 5.4pt 0in 5.4pt; height:13.9pt"
            width="27" valign="bottom" nowrap="nowrap">
            <p class="MsoNormal" style="text-align:center" align="center"><span
                    style="font-size:12.0pt; font-family:&quot;Arial&quot;,sans-serif">40</span></p>
        </td>
        <td style="width:20.15pt; border-top:none; border-left:none; border-bottom:solid windowtext 1.0pt; border-right:solid windowtext 1.0pt; padding:0in 5.4pt 0in 5.4pt; height:13.9pt"
            width="27" valign="bottom" nowrap="nowrap">
            <p class="MsoNormal" style="text-align:center" align="center"><span
                    style="font-size:12.0pt; font-family:&quot;Arial&quot;,sans-serif">40</span></p>
        </td>
        <td style="width:17.0pt; border-top:none; border-left:none; border-bottom:solid windowtext 1.0pt; border-right:solid windowtext 1.0pt; padding:0in 5.4pt 0in 5.4pt; height:13.9pt"
            width="23" valign="bottom" nowrap="nowrap">
            <p class="MsoNormal" style="text-align:center" align="center"><span
                    style="font-size:12.0pt; font-family:&quot;Arial&quot;,sans-serif">40</span></p>
        </td>
        <td style="width:.3pt; padding:0in 0in 0in 0in; height:13.9pt" width="0"></td>
    </tr>
    <tr style="height:13.15pt">
        <td style="width:42.15pt; border-top:none; border-left:none; border-bottom:solid windowtext 1.0pt; border-right:solid windowtext 1.0pt; padding:0in 5.4pt 0in 5.4pt; height:13.15pt"
            width="56" valign="bottom" nowrap="nowrap">
            <p class="MsoNormal" style="text-align:center" align="center"><span
                    style="font-size:12.0pt; font-family:&quot;Arial&quot;,sans-serif; color:black">2</span></p>
        </td>
        <td style="width:88.75pt; border-top:none; border-left:none; border-bottom:solid windowtext 1.0pt; border-right:solid windowtext 1.0pt; padding:0in 5.4pt 0in 5.4pt; height:13.15pt"
            width="118" valign="bottom" nowrap="nowrap">
            <p class="MsoNormal" style="text-align:center" align="center"><span
                    style="font-size:12.0pt; font-family:&quot;Arial&quot;,sans-serif">60</span></p>
        </td>
        <td style="width:20.15pt; border-top:none; border-left:none; border-bottom:solid windowtext 1.0pt; border-right:solid windowtext 1.0pt; padding:0in 5.4pt 0in 5.4pt; height:13.15pt"
            width="27" valign="bottom" nowrap="nowrap">
            <p class="MsoNormal" style="text-align:center" align="center"><span
                    style="font-size:12.0pt; font-family:&quot;Arial&quot;,sans-serif">40</span></p>
        </td>
        <td style="width:20.15pt; border-top:none; border-left:none; border-bottom:solid windowtext 1.0pt; border-right:solid windowtext 1.0pt; padding:0in 5.4pt 0in 5.4pt; height:13.15pt"
            width="27" valign="bottom" nowrap="nowrap">
            <p class="MsoNormal" style="text-align:center" align="center"><span
                    style="font-size:12.0pt; font-family:&quot;Arial&quot;,sans-serif">40</span></p>
        </td>
        <td style="width:17.0pt; border-top:none; border-left:none; border-bottom:solid windowtext 1.0pt; border-right:solid windowtext 1.0pt; padding:0in 5.4pt 0in 5.4pt; height:13.15pt"
            width="23" valign="bottom" nowrap="nowrap">
            <p class="MsoNormal" style="text-align:center" align="center"><span
                    style="font-size:12.0pt; font-family:&quot;Arial&quot;,sans-serif">40</span></p>
        </td>
    </tr>
    </tbody>
</table>
 类似资料:
  • 问题内容: 我正在尝试将通过BeautifulSoup提取的表转换为JSON。 到目前为止,尽管我不确定如何从此处处理数据,但我设法隔离了所有行。任何建议将不胜感激。 (为了方便阅读,换行了) 这是我的尝试: 这给了我以下结果: 问题答案: 您的数据可能类似于: 我们可以使用以下代码从中以列表的形式获得您的结果: 要将结果转换为JSON(如果您不关心顺序): 结果: 如果您需要相同的订单,请使用以

  • 问题内容: 是否有任何jQuery或javascript库根据给定的json数据生成动态表?我不想定义列,该库应该读取json哈希中的键并生成列。 当然,我可以自己遍历json数据并生成html表。我只想知道是否存在可以简单重用的此类库。 问题答案: 感谢大家的答复,这使用jQuery。 程式码片段:

  • 问题内容: 我有一个看起来像这样的JSON对象: 我想将其转换为如下所示的HTML表: 谁能告诉我如何实现这一目标? 问题答案: 使用jQuery非常简单: 这是工作提琴的链接。 更新: 实现此目的的另一种方法是使用称为dynatable的库将JSON转换为可排序的表。

  • 问题内容: 有没有一种真正简单的方法可以获取JSON对象数组并将其转换为HTML表(不包括几个字段)?还是我必须手动执行此操作? 问题答案: 我不确定这是否是您想要的,但是有jqGrid。它可以接收JSON并生成网格。

  • 有没有jQuery或javascript库生成给定json数据的动态表?我不想定义列,库应该读取json散列中的键并生成列。 当然,我可以自己遍历json数据并生成html表。我只是想知道是否有这样的库存在,我可以简单地重复使用。

  • 问题内容: 我在此上浪费了很多时间。递归部分非常虚幻。 对于未知深度的给定HTML结构,我需要转换为JSON。 (我从正在构建的某些YAML i18n翻译系统中使用此功能) 我的一般想法是深入了解,直到找到,然后使用的键/值创建一个对象,然后返回该对象,因此它将是最后到达的KEY的值。 (是的,开发有点复杂,但非常有趣) JSBIN游乐场 -实时代码示例 我无法让递归函数正常工作,无法输出所需的J