list]
如何将Json里的数组一次全部遍历出来, 现在是一个小小的例子,请参考
如果想精通JSon 到官方
[url]http://www.json.org[/url]
[/list]
[list]
[[color=olive]***********************************************************[/color]]
[/list]
[list]
[下面是一段通用的JS 代码]
[/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;
}