1

如果我遍历我的 html 表中的所有单元格并将每个 rowOBj 添加到 tableOBJ 中的一个新属性,它看起来像下面这样但是......

        var tableOBJ = {};
        $("table tr").each(function (index, value) {

            var r = new rowOBJ(
                                    $(this).find('td').eq(0).text(),
                                    $(this).find('td').eq(1).text()
                                    );

            tableOBJ[index] = r;

        });


        var p = JSON.stringify(tableOBJ);


p =
    {
    "0":{"name":"fdgd","surname":"ssdt"},
    "1":{"name":"fdsf","surname":"vn"},
    "2":{"name":"dfsb","surname":"mry"},
    "3":{"name":"hsdsdfry","surname":"smh"}
    }

如何使以前的外观使 json 看起来像这样

    {
    {"name":"fdgd","surname":"ssdt"},
    {"name":"fdsf","surname":"vn"},
    {"name":"dfsb","surname":"mry"},
    {"name":"hsdsdfry","surname":"smh"}
    }
4

1 回答 1

5

如果 tableOBJ 是一个数组会有帮助吗?

var tableOBJ = [];

然后在你的循环中而不是设置对象的索引推送数组:

tableOBJ.push(r);
于 2012-08-01T19:24:06.333 回答