Loop Json Array

list]
如何将Json里的数组一次全部遍历出来, 现在是一个小小的例子,请参考
如果想精通JSon 到官方
[url]http://www.json.org[/url]

[/list]

function loopJson() {
var results = {"d":[{"__type":"FluentWeb.DTO.EmployeeOrder",
"EmployeeName":"Janet Leverling",
"EmployeeTitle":"Sales Representative",
"RequiredDate":"\/Date(839224800000)\/",
"OrderedProducts":null}]}
var result = [ {"id":1,"name":"zhangsan",
"address":{"homeAddress":"beijing",
"companyAddress":"shanghai"},
"friends":[{"id":5,"name":"zhangsan\u0027s friend1"},
{"id":6,"name":"zhangsan\u0027s friend2"}]
},
{"id":2,"name":"lisi","address":{"homeAddress":"liaoning","companyAddress":"fujian"},
"friends":[{"id":10,"name":"lisi\u0027s friend1"},
{"id":11,"name":"lisi\u0027s friend2"}]
}
];
for (var i = 0; i < result.length; i++) {
for (var prop in result[i]) {
if(typeof result[i][prop] == 'object') {
loopJSONData(result[i][prop]);
}else {
alert(result[i][prop]);
}
}
}


}

function loopJSONData(result) {
for(var prop in result) {
if(typeof result[prop] == 'object') {
loopJSONData(result[prop]);
}else {
alert(result[prop]);
}
}
}


<body id="theBody">

<input type="button" value="loopJson " onclick="loopJson()">

</body>

[list]
[[color=olive]***********************************************************[/color]]
[/list]
[list]
[下面是一段通用的JS 代码]
[/list]

/**
* The method resolving json result data.
* Only get requests call it.
* Invoke this method require comply json reference:
* [{key: value} , {key : value, object : [{key : value.......}]}, {key : value}........]
*/
function getRequestJson(url, header, elementId) {
$.getJSON(url,function(results, status){
if("success" == status){
loopJSONData(results, header, elementId);
}
});
}

/**
* The method resolving json result data.
* Only post requests call it.
* Invoke this method require comply json reference:
* [{key: value} , {key : value, object : [{key : value.......}]}, {key : value}........]
*/
function postRequestJson(url, header, elementId) {
$.post(url,{}, function(returnedData, status) {
if("success" == status) {
loopJSONData(results, header, elementId);
}
});
}

function loopJSONData(results, header, elementId) {
var html = "<table width='100%' border = '1' align='left' id='jsonTable'>";
html += createHeader(header);
var k = 0;
var n = 0;
for (var i = 0; i < results.length; i++) {
html += "<tr>";
for (var prop in results[i]) {
if(typeof results[i][prop] == 'object') {
if(results[i][prop].constructor == Array && typeof results[i][prop] == 'object') {
html += loopJsonChildArray(results[i][prop]);
}else {
html += loopJSONChildObject(results[i][prop])
}
}else {
html += "<td>"+results[i][prop]+"</td>";
}
}
html += "</tr>";
}
html += "</table>";
$("#"+elementId+"").append(html);
}

function loopJSONChildObject(results) {
var html = "";
for(var prop in results) {
if(typeof results[prop] == 'object') {
html += loopJSONChildObject(results[prop]);
}else {
html += "<td>"+results[prop]+"</td>";
}
}
return html;
}

function loopJsonChildArray(results) {
var html = "";
for(var i = 0; i < results.length; i++ ) {
html += "<tr>";
for(var prop in results[i]) {
if(typeof results[i][prop] == 'object') {
if(results[i][prop].constructor == Array) {
html += loopJSONChildObject(results[i][prop]);
}else {
html += loopJSONChildObject(results[i][prop]);
}
}else {
html += "<td>"+results[i][prop]+"</td>";
}
}
html += "</tr>";
}
return html;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值