0

我有从 URL 返回的 JSON 对象,格式为

<pre>
    [{"merchant":"eletspay","accountName":"hello","accountType":"Savings","ifscCode":"5545","email":"mypal.1989@gmail.com","phoneNum":"98654567876","isDefaultAccount":false},{"merchant":"eletspay","accountName":"helloi32irn","accountType":"Savings","ifscCode":"5545","email":"mypal.appa99721989@gmail.com","phoneNum":"98654567876","isDefaultAccount":false}]
    </pre>

我想循环遍历每个索引的 JSON 对象,以显示该表所在的 html 表中的所有内容

<pre>
<table class= "dataTable">
                    <tr>
                    <td><input type="radio" name="activate"></td>
                    <td>jsonObject[1.value]</td>
                    <td>jsonObject[1.value]</td>
                    <td>jsonObject[1.value]</td>
                    <td>jsonObject[1.value]</td>
                    <td>jsonObject[1.value]</td>
                    <td>jsonObject[1.value]</td>

                    </tr>
<tr>
                    <td><input type="radio" name="activate"</td>
                    <td>jsonObject[2.value]</td>
                    <td>jsonObject[2.value]</td>
                    <td>jsonObject[2.value]</td>
                    <td>jsonObject[2.value]</td>
                    <td>jsonObject[2.value]</td>
                    <td>jsonObject[2.value]</td>
                                            </tr>
</table>
</pre>

jsonObject[1.value]索引 1 处的数据在哪里jsonObject[2.value],索引 2 处的数据在哪里,依此类推。

PS:我是 Javascript 和 Jquery 的新手,所以请纠正/纠正 html 部分中的任何愚蠢错误。

4

2 回答 2

1

试试这个http://jsfiddle.net/aBZDg/13/

var jsonData = [{"merchant":"eletspay","accountName":"hello","accountType":"Savings","ifscCode":"5545","email":"mypal.1989@gmail.com","phoneNum":"98654567876","isDefaultAccount":false},{"merchant":"eletspay","accountName":"helloi32irn","accountType":"Savings","ifscCode":"5545","email":"mypal.appa99721989@gmail.com","phoneNum":"98654567876","isDefaultAccount":false}];

function writeTable() {
    var out = '';

    for(var row=0; row<jsonData.length; row++) {
        out += '<tr>'
                + '<td>' + jsonData[row].merchant + '</td>'
                + '<td>' + jsonData[row].accountName + '</td>'
                + '<td>' + jsonData[row].accountType + '</td>'
                + '<td>' + jsonData[row].ifscCode + '</td>'
                + '<td>' + jsonData[row].email + '</td>'
                + '<td>' + jsonData[row].phoneNum + '</td>'
                + '<td>' + jsonData[row].isDefaultAccount + '</td>'
                + '</tr>';
    }

    document.write(out);
}​

然后在要显示表格的地方调用此函数。

<table border="1">
    <tr>
        <th>Merchant</th>
        <th>AccountName</th>
        <th>AccountType</th>
        <th>IfscCode</th>
        <th>Email</th>
        <th>PhoneNum</th>
        <th>IsDefaultAccount</th>
    </tr>
    <script>writeTable();</script>
</table>
于 2012-10-23T08:52:23.313 回答
0

尝试使用 jquerytemplate

http://www.diplo.co.uk/blog/2011/3/1/using-jquery-templates-to-bind-to-json-data.aspx

这将循环从您的 url 返回的 json 数据

于 2012-10-23T08:30:40.097 回答