如何从JSON嵌套对象获取值

时间:2019-11-21 06:08:44

标签: javascript arrays json

{
  "Petition to file": [
    {
      "description": "fileing description",
      "period": "30 days",
      "fees": "500"
    }
  ],
  "Appearing before inspection": [
    {
      "description": "Appearence Description",
      "period": "10 days",
      "fees": "1500"
    }
  ],
  "Passing orders for a recording of statements": [
    {
      "description": "passing order description",
      "period": "50 days",
      "fees": "1000"
    }
  ],
  "Hearing of petition": [
    {
      "description": "Hearing of petition description",
      "period": "55 days",
      "fees": "2000"
    }
  ],
  "Decree of petition": [
    {
      "description": "decreeof description",
      "period": "70 days",
      "fees": "5000"
    }
  ]
}

如何获取嵌套子标题的值。这是动态的内容。此外,我还需要获取period,fess,description的嵌套对象的值。我正在使用角度cli中的打字稿程序

3 个答案:

答案 0 :(得分:1)

这是完整的工作示例StackBlitz Link,遍历动态对象条目的主要逻辑是...

ngOnInit(){
    Object.keys(this.datafromDatabase).forEach(data =>{ 
         this.datafromDatabase[data].map(key =>{
           this.data.push(key)
         });
    })
}

您的模板文件为...

<div *ngFor="let key of data">
    {{key.description}}
</div>
<div>
     {{data |json}}
</div>

答案 1 :(得分:0)

something

通常就是在键中获取值的方法

现在,新变量[ { "description": "fileing description", "period": "30 days", "fees": "500" } ] =

description

现在是否要var descriptionVariable = consts['description'];

fileing description

所以descriptionVariabe的值为{{1}}

答案 2 :(得分:0)

let list = {
  "Petition to file": [
    {
      "description": "fileing description",
      "period": "30 days",
      "fees": "500"
    }
  ],
  "Appearing before inspection": [
    {
      "description": "Appearence Description",
      "period": "10 days",
      "fees": "1500"
    }
  ],
  "Passing orders for a recording of statements": [
    {
      "description": "passing order description",
      "period": "50 days",
      "fees": "1000"
    }
  ],
  "Hearing of petition": [
    {
      "description": "Hearing of petition description",
      "period": "55 days",
      "fees": "2000"
    }
  ],
  "Decree of petition": [
    {
      "description": "decreeof description",
      "period": "70 days",
      "fees": "5000"
    }
  ]
}


for (let [key, value] of Object.entries(list)) {
 	console.log(key);
	console.log(value);
}

使用Object.entries