首页 > 解决方案 > 将嵌套的 json 分配给变量

问题描述

我有以下界面:

export interface IdataTypes {
dataType: {
  Categories: string[],
  isSelected: boolean,
}
}

和以下 JSON

{
  "Sensitive Personal Data": {
    "isSelected": "false",
    "legalReference": "",
    "Categories": [
      "Health Data",
      "Genetic Data",
      "Biometric Data",
      "Trade Union Data",
      "Religious Data",
      "Political Opinion",
      "Philosophical Belief",
      "Racial or Ethic Origin",
      "Sex Life",
      "Sexual Orientation"
    ]
  },
"Personal Data with Higher Risk":{
  "isSelected":"false",
  "legalReference":"",
  "Categories":["location","Financial/Social Status","Data Related to Profiling",
    "video surveillance","data on adoption and custody"]
}


}

我想读取文件的“dataTypeCategories”并将其分配给接口文件的变量。所以我做以下

 for(let i=0; i<response["Personal Data with Higher Risk"].Categories.length;i++){
        this.dataTypeHighRisk["Personal Data with Higher Risk"][i].Categories.push
        (response["Personal Data with Higher Risk"].Categories[i]);
        this.dataTypeHighRisk["Personal Data with Higher Risk"][i].isSelected=false;
       ;
      }

我收到此错误:core.js:1542 ERROR 错误:未捕获(承诺中):ReferenceError:未定义类别

你能告诉我怎么做吗?我是 javascript 和 angular 的新手。

更新数组定义为:dataTypeHighRisk: IdataTypes[]=[]; 我不知道如何定义属性“具有高风险的个人数据/

标签: jsonangulartypescriptmean-stack

解决方案


推荐阅读