Angular 2将JSON对象解析为角度类

zan*_*her 25 json jsonp typescript angular

我一直在关注Angular2入门文档,让我自己的应用程序开始,我已经成功地从本地文件中检索JSON对象并在angular2模板中显示它们.但是目前它依赖于与我的JSON文件中的对象结构完全匹配的角度类.

最终我需要我的应用程序使用JSON-LD,它具有诸如"@id"的属性名称.我做了一些谷歌搜索,似乎RxJS可能有我正在寻找的功能,但我不知道从哪里开始中断自动绑定从一些JSON数据直接到我的angular2类,而是能够看到在json中标记为"@id"的东西并设置SomeClass.id的值

当前代码自动生成Person类的数组:

getPeople(){
    return this._http.get(this._peopleUrl)
        .map(response => <Person[]> response.json().data)
        .do(data => console.log(data)) //debug to console
        .catch(this.handleError);
} 
Run Code Online (Sandbox Code Playgroud)

这样的JSON很好:

[
 {
  "id":"1",
  "name":"tessa"
 },
 {
  "id":"2",
  "name":"jacob"
 }
]
Run Code Online (Sandbox Code Playgroud)

但是对于这样的JSON来说显然会失败:

[
 {
  "@id":"1",
  "name":"tessa"
 },
 {
  "@id":"2",
  "name":"jacob"
 }
]
Run Code Online (Sandbox Code Playgroud)

我的课很简单(目前:-))

export class Person {
  constructor(
    public id:number,
    public name:string,
  ){}
}
Run Code Online (Sandbox Code Playgroud)

任何人都可以指出我正确的方向为一些文件/例子将复杂/棘手的json映射到angular2中的类?

zan*_*her 11

我在这里给出答案的选项4中找到了答案:如何使用JSON对象初始化typescript对象

希望这有助于如果有人登陆此页面寻找同样的事情!