我试图以递归方式迭代JSON结构。我检查了其他论坛/ stackoverflow问题,但我似乎有一个奇怪的问题 那么来自REST服务的是:
{/Envelope=
{/Envelope/Header = {},
/Envelope/Body =
{/Envelope/Body/ns:GetAddressForPostCodeResponse=
{/Envelope/Body/ns:GetAddressForPostCodeResponse/ns:maximumAddressMatches = ? ,
/Envelope/Body/ns:GetAddressForPostCodeResponse/ns:postalAddress =
{/Envelope/Body/ns:GetAddressForPostCodeResponse/ns:postalAddress/ns:apartment = ? ,
...
基本上,我使用String键从SpringMVC发送一个Map,并将另一个Map作为值。
(有角度)代码:
$scope.getHtml = function(node, isAdd) {
if (isAdd) {
var html = "<ul id='tree1'>";
} else {
var html = "<ul>";
}
//angular.forEach(node, function(value, key) {
for (var key in node) {
if (node.hasOwnProperty(key)) {
var value = node[key];
console.log(key + " : " + value);
console.log(node.hasOwnProperty(key));
if (key != "$promise" && key != "$resolved") {
html += "<li><input type='checkbox' id='" + key + "'><label>" + key + "</label>";
html += $scope.getHtml(value, false);
}
}
//});
}
html += "</ul>";
delete node;
return html;
};
正如您所看到的,有一个递归调用。
我在控制台中可以看到的内容:
true serviceDiscovery.controller.js:39
"0 : ?" serviceDiscovery.controller.js:38
true serviceDiscovery.controller.js:39
"0 : ?" serviceDiscovery.controller.js:38
... (the same two rows some thousands times)
"0 : ?" serviceDiscovery.controller.js:38
true serviceDiscovery.controller.js:39
"Error: too much recursion
$scope.getHtml/<@http://localhost:8080/management-app-1.0.0-SNAPSHOT/resources/js/serviceDiscovery.controller.js:37:21
r@http://localhost:8080/management-app-1.0.0-SNAPSHOT/resources/js/angularjs/angular.min.js:7:288
所以有一个无限循环但是如何......?我也试过了angular.forEach。 有什么想法吗?
-------------更新-------------
嗯,我的错,抱歉。如果值是map(对象)而不是String,我只需要递归调用。