【问题标题】:Convert json data into an object in angular 2+ [duplicate]将json数据转换为角度2+的对象[重复]
【发布时间】:2021-02-09 20:20:22
【问题描述】:

如何将此json文件中的数据转换为对象并显示在表格中

{
"AndraPradesh": {
"dData": {
  "Anantapur": {
    "notes": "",
    "history":2
    "delta": {
      "force": 0
    }
  }
},
"sCode": "AP"
},

"Maharastra": {
"dData": {
  "Pune": {
    "notes": "",
    "history":2
    "delta": {
      "force": 0
    }
  }
},
"sCode": "MH"
},

}

我创建了如下界面

export interface DataInterface {
    sName: string[];
    dName: dData[];
    sdCode: string;
}

export interface dData{
    name: string;
    notes: string;
    history: number;
    delta: Delta[];
}

interface Delta {
    force: number;
}

我还创建了一个类文件。我不确定是否可以将上述 json 转换为对象,以便将其分配给任何变量以进行进一步操作

【问题讨论】:

    标签: json angular typescript


    【解决方案1】:

    我注意到你的 DataInterface.dName 是一个数组,同时它不是你显示的。

    在这里查看答案:https://stackoverflow.com/a/40421495/14498619

    let jsonObj: any = JSON.parse(employeeString); // string to generic object first
    let employee: Employee = <Employee>jsonObj;
    

    【讨论】:

      猜你喜欢
      • 2017-11-24
      • 2016-07-27
      • 1970-01-01
      • 2014-11-05
      • 2018-04-08
      • 2012-10-09
      • 2021-05-12
      • 1970-01-01
      • 2021-05-26
      相关资源
      最近更新 更多