简体   繁体   中英

How to consume the complex json nested properties

here i have a dynamic json

data = {
  "name": "deltha",
  "type": "object",

  "important": [
    "name",
    "id",
    "number"
  ],

  "information": {
    "place": {
      "editable": false,
      "visible": true
    },
    "info": {
      "type": "object",
      "properties": {
        "type": {
          "visible": true

        }
      }
    },
    "Image": {
      "required": [
        "name"
      ],
      "type": "object",
      "properties": {
        "deltha": {
          "search": "yes"
        }
      }
    }

  }
}

here i am trying to check whether each and every nested property has "required" attribute or not

for ex

data['information']["Image"]

here from the above object i have a attribute i have "required" and under that "name" is there suppose like image how can i check each and every property to check there is 'required' if required there then how can i read that value dynamically

use the hasOwnProperty check the property exist or not

let obj = data['information']["Image"];

if(obj.hasOwnProperty('required')){
   console.log(obj.required)
}

我建议使用递归函数,这是一个工作示例: stackblitz.com/edit/angular-snucnm

you can check the availability of the property as follows,

if (data.information.Image.required !== undefined) {
   console.log('prop is defined')
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM