如何在AngularJs中获取json对象和嵌套对象属性名称和值?

时间:2016-03-21 02:38:42

标签: javascript angularjs json

我有一个像这样的json对象

{
  "from": {
    "required": false,
    "type": {
      "name": {
        "required": false,
        "type": "String"
      },
      "email": {
        "required": true,
        "type": "String"
      }
    }
  },
  "to": {
    "required": true,
    "type": [
      {
        "name": {
          "required": false,
          "type": "String"
        },
        "email": {
          "required": true,
          "type": "String"
        }
      }
    ]
  },
  "subject": {
    "required": true,
    "type": "String"
  },
  "text": {
    "required": true,
    "type": "String"
  },
  "html": {
    "required": true,
    "type": "String"
  }
}

该对象包含嵌套对象,具有从各种服务设置的属性。因此,属性名称是动态更改的。我想获得属性名称和各自的值。我已经尝试过以下代码。

 for (var prop in data) {
                $scope.fieldsInfo.push({ "label": prop, "required":data.hasOwnProperty(prop) });
            }

在上面的代码中我也获取了属性名称(来自)和所需的值,现在我想在名称对象中获取嵌套对象属性名称(名称,电子邮件)以及(必需,类型)值。在上面的对象from中包含type是一个对象,因此isArray:false和下一个对象to包含type是一个数组,所以isArray:true剩余的对象{ {1}}是一个字符串type。 像这样的输出

isArray:false

2 个答案:

答案 0 :(得分:1)

我不确定你想要什么,但根据你的问题的标题,我将提供一些处理javascript对象/数组的基础知识:

使用for (var prop in data) {}(与您一样)对键进行迭代不适用于嵌套目标。一种可能的方法是使用递归函数:

function get_keys_recursive (object_or_array) {

    for (var key in object)
        if (object[key] != null) //null is also a object
            if (typeof object[key] === 'object') 
                get_keys_recursive (typeof object[key]); //recursive function call
            else {

                console.log (key); //here you get all keys
                console.log (object[key]); //here you get the value 

            }

}

get_keys_recursive (your_object); //function call

如果没有回答您的问题,请提供更详细的信息。

答案 1 :(得分:1)

$scope.fieldsInfo = [];
            $scope.type = [];
            for (var prop in data) {
                var isArray = angular.isArray(data[prop].type);
                var isObject = angular.isObject(data[prop].type);
                if (isArray) {
                    for (var nestedProp in data[prop].type[0]) {
                        $scope.type.push({ "label": nestedProp, "required": result.data.hasOwnProperty(nestedProp), "type": data[prop].type[0][nestedProp].type });
                    }
                    $scope.fieldsInfo.push({ "label": prop, "required": result.data.hasOwnProperty(prop), "isArray": isArray, "type": $scope.type });
                    $scope.type = [];
                }
                else
                    if (isObject) {
                        for (var nestedProp in data[prop].type) {
                            $scope.type.push({ "label": nestedProp, "required": data.hasOwnProperty(nestedProp), "type": data[prop].type[nestedProp].type });
                        }
                        $scope.fieldsInfo.push({ "label": prop, "required": result.data.hasOwnProperty(prop), "isArray": isArray, "type": $scope.type });
                        $scope.type = [];
                    }
                else {
                    $scope.fieldsInfo.push({ "label": prop, "required": data.hasOwnProperty(prop), "isArray": isArray, "type": data[prop].type });
                }
            }