自动将 JSON 对象映射到 TypeScript 对象 (Angular 2)
Map JSON object to TypeScript object automatically (Angular 2)
我是 Typescript 的新手,javascript。
我的问题是:
如何从这个
```
{
"release": {
"ref": {},
"commits": {}
},
"debug": {
"ref": {},
"commits": {}
},
"feature": {
"ref": {},
"commits": {}
},
"hotfix": {
"ref": {},
"commits": {}
}
}
```
使用接口将其与 Typescript 映射
export interface IRepo {
branches: IBranch; // how to make branches a of type string: IBranch dictionary?
}
将有未知数量的属性,如调试、发布等,
我想将它们作为字典保存在 IRepo 实例中
我正在使用它来获取数据:
```
getRepo() : Observable<IRepo> {
if (!this.repo) {
return this.http.get(this._baseUrl + 'repo.json')
.map((res: Response) => {
this.repo = res.json();
return this.repo;
})
.catch(this.handleError);
} else {
return this.createObservable(this.repo);
}
}
```
你有一张地图,从一些 已知 string
到一个已知结构的对象(有 ref
commits
)。只需使用字符串索引器:
interface BranchByName {
[branchName: string]: {
ref: any;
commits: any;
}
}
我是 Typescript 的新手,javascript。 我的问题是: 如何从这个
```
{
"release": {
"ref": {},
"commits": {}
},
"debug": {
"ref": {},
"commits": {}
},
"feature": {
"ref": {},
"commits": {}
},
"hotfix": {
"ref": {},
"commits": {}
}
}
```
使用接口将其与 Typescript 映射
export interface IRepo {
branches: IBranch; // how to make branches a of type string: IBranch dictionary?
}
将有未知数量的属性,如调试、发布等, 我想将它们作为字典保存在 IRepo 实例中
我正在使用它来获取数据:
```
getRepo() : Observable<IRepo> {
if (!this.repo) {
return this.http.get(this._baseUrl + 'repo.json')
.map((res: Response) => {
this.repo = res.json();
return this.repo;
})
.catch(this.handleError);
} else {
return this.createObservable(this.repo);
}
}
```
你有一张地图,从一些 已知 string
到一个已知结构的对象(有 ref
commits
)。只需使用字符串索引器:
interface BranchByName {
[branchName: string]: {
ref: any;
commits: any;
}
}