递归地将表数据转换为JSON

时间:2014-08-13 10:05:38

标签: javascript json

我需要打开一个看起来像这样的表:

    <table class="pie-chart-data" border="1">
        <thead>
            <tr>
                <th>has_title</th>
                <th>has_value</th>
                <th>has_details</th>
                <th>has_overflow</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td rowspan="3">Cycling</td>
                <td rowspan="3">2</td>
                <td>Lorem ipsum</td>
            </tr>
            <tr>
                <td>Dolor sit amet</td>
            </tr>
            <tr>
                <td>Consecetitur</td>
            </tr>

            <tr>
                <td rowspan="3">Skating</td>
                <td rowspan="3">3</td>
                <td>Lorem</td>
                <td rowspan="3">
                    <table border="1">
                        <thead>
                            <tr>
                                <th>has_title</th>
                                <th>has_value</th>
                                <th>has_details</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td rowspan="3">Cycling</td>
                                <td rowspan="3">2</td>
                                <td>Lorem ipsum</td>
                            </tr>
                            <tr>
                                <td>Dolor sit amet</td>
                            </tr>
                            <tr>
                                <td>Consecetitur</td>
                            </tr>
                        </tbody>
                    </table>
                </td>
            </tr>
            <tr>
                <td>Ipsum</td>
            </tr>
            <tr>
                <td>Dolor</td>
            </tr>
        </tbody>

    </table>

进入如下所示的JSON:

var data = [
                {
                    title: 'Cycling',
                    value: 2,
                    details: ['186 unique individuals', '2.2% of total', 'Extra information area']
                },
                {
                    title: 'Skating',
                    value: 3,
                    details: ['Lorem ipsum', 'Dolor sit amet', 'Extra information area']
                },
                {
                    title: 'Other',
                    value: 1,
                    overflow: [
                        {
                            title: 'Overflow 1',
                            value: 1,
                            details: ['186 unique individuals', '2.2% of total', 'Extra information area']
                        },
                        {
                            title: 'Overflow 2',
                            value: 3,
                            details: ['186 unique individuals', '2.2% of total', 'Extra information area']
                        }
                    ]
                },
                {
                    title: 'Climbing',
                    value: 4,
                    details: ['186 unique individuals', '2.2% of total', 'Extra information area']
                },
                {
                    title: 'Kayaking',
                    value: 2,
                    details: ['186 unique individuals', '2.2% of total', 'Extra information area']
                },
                {
                    title: 'Skydiving',
                    value: 5,
                    details: ['186 unique individuals', '2.2% of total', 'Extra information area']
                },
                {
                    title: 'Rowing',
                    value: 2,
                    details: ['186 unique individuals', '2.2% of total', 'Extra information area']
                },
                {
                    title: 'Rowing',
                    value: 3,
                    details: ['186 unique individuals', '2.2% of total', 'Extra information area']
                }
            ];

详细项目的数量可能会有所不同,因此我假设我需要查看rowspan属性并推断下一个值的位置。此外,表格中还包含一个表格。将嵌入“溢出”中。属性,所以我也假设我需要使用递归来从这些嵌套表中提取键/值。

有人能指出我这样做的正确方向吗?

0 个答案:

没有答案