【问题标题】:How to get json object and nested object Property names and values in AngularJs?如何在 AngularJs 中获取 json 对象和嵌套对象的属性名称和值?
【发布时间】:2016-03-21 02:38:42
【问题描述】:

我有一个这样的 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 其余对象type 是一个字符串所以isArray:false。 像这样输出

$scope.fieldsInfo=[];

  $scope.fieldsInfo=[
                      { 
                       "label"    :"from",
                       "required" :false,
                       "isArray"  :false,
                       "type"     : [
                                      {
                                        "label"   :"name",
                                        "type"    : "String",
                                        "required": false
                                      }
                                      {
                                        "label"   : "email",
                                        "type"    : "String",
                                        "required": true 
                                      }                     
                                    ]
                      },
                      { 
                       "label"    :"to",
                       "required" :true,
                       "isArray"  :true,
                       "type"     : [
                                      {
                                        "label"   : "name",
                                        "type"    : "String",
                                        "required": false
                                      }
                                      {
                                        "label"   : "email",
                                        "type"    : "String",
                                        "required": true 
                                      }                       
                                    ]
                      },
                      { 
                       "label"    :"subject",
                       "required" :true,
                       "isArray"  :false,
                       "type"     :"String" 
                      },
                      { 
                       "label"    :"text",
                       "required" :true,
                       "isArray"  :false,
                       "type"     :"String" 
                      },
                      { 
                       "label"    :"html",
                       "required" :true,
                       "isArray"  :false,
                       "type"     :"String" 
                      }
                    ]

【问题讨论】:

    标签: javascript angularjs json


    【解决方案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
    

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

    【讨论】:

      【解决方案2】:
      $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 });
                      }
                  }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-08-23
        相关资源
        最近更新 更多