我从格式为
的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部分中进行/纠正任何愚蠢的错误。
答案 0 :(得分: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>
答案 1 :(得分:0)
尝试使用jquerytemplate
http://www.diplo.co.uk/blog/2011/3/1/using-jquery-templates-to-bind-to-json-data.aspx
这将循环从您的网址
返回的json数据