您的位置:首页 > Web前端 > JavaScript

使用递归对任意json解析

2015-11-19 17:53 585 查看
(function(angular){

var app = angular.module('app', []);

app.controller('MainCtrl', [

'$scope', '$http', function($scope, $http) {

var url ="http://localhost:8080/test/getData";

$http.get(url)

.success(function (datas) {

var space="|";

var Objs = eval(datas);

myparseJson(Objs,space);

});

function myparseJson(datas,space){

if(isJson(datas)){

for(key in datas){

var value = datas[key];

console.info(space + key+":");

var tempspace = space+" |";

myparseJson(value,tempspace);

}

}else if(isArray(datas)){

for(index in datas){

var value = datas[index];

var tempspace = space + " |";

myparseJson(value,tempspace);

}

}else if(isNull(datas)){

console.info(space + 'nulltt');

return '';

}else if(datas == undefined){

console.info(space + 'undefinedtt');

return '';

}else{

console.info(space + datas);

return datas;

}

}

function isJson(obj){

var isjson = typeof(obj) == "object" && Object.prototype.toString.call(obj).toLowerCase() == "[object object]" && !obj.length;

return isjson;

}

function isArray(obj){

var isarray = typeof(obj) == "object" && Object.prototype.toString.call(obj).toLowerCase() == "[object array]" && obj.length;

return isarray;

}

function isNull(obj){

var isnull = typeof(obj) == "object" && Object.prototype.toString.call(obj).toLowerCase() == "[object null]";

return isnull;

}

}]);

})(window.angular);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: