如何在angularjs中解析动态变化的json
我有一个动态变化的json,如下面的示例所示 例1:如何在angularjs中解析动态变化的json,angularjs,Angularjs,我有一个动态变化的json,如下面的示例所示 例1: "Flower":{ "color": "red" } 例2: "Flower":{ "color": "red" , "seasonal": "yes" } 例3: "Flower":{ "color": "red" , "seasonal": { "seasons":{
"Flower":{
"color": "red"
}
例2:
"Flower":{
"color": "red" ,
"seasonal": "yes"
}
例3:
"Flower":{
"color": "red" ,
"seasonal": {
"seasons":{
"winter": "yes",
"summer" : "no"
}
}
}
对于上面的例子,输出应该是
example1 : color:red
example2 :color:red,seasonal:yes
example3: color:red,seasonal.seasons.winter:yes,seasonal.seasons.summer:no
这里的问题是不知道每次都会发生什么,它应该是输出格式。我需要javascript中的一些逻辑:(提供isObject和isString等函数,它们将告诉您变量是对象还是字符串 您可以遍历对象的属性并检查该属性是对象还是字符串。如果是对象,则可以递归该对象的属性,否则只需打印它。请检查以下内容
$scope.iterate = function(object) {
if (!angular.isObject(object))
return object;
var opt = "";
for (var property in object) {
if (object.hasOwnProperty(property)) {
opt = opt + property + " - " + $scope.iterate(object[property]) + " ";
}
}
return opt;
}
你也可以看看这把小提琴,它有一个上面代码的工作解决方案。如果你真的不知道结构将是什么,你可以使用递归。非常感谢