我的场景非常简单。我正在尝试反序列化Angular2应用程序中的JSON。问题是在JSON中有一个以@开头的字段。
示例:
{
"@type": ".StringField",
"name": "description",
"type": "String",
"value": "A"
}所以我的问题是,我如何在Typescript中编写一个带有字段@type的类?有没有办法干净利落地做这件事?我想不出解决办法。
@Günter Zöchbauer
考虑以下代码:
getCustomerProfile(): Observable<CustomerMetaModel> {
return this.http.get('/customer/metaModel')
.map(this.extractData)
.catch(this.handleError);
}
private extractData(res: Response): string {
let body = res.json();
return body || {};
}解析不是我一个人做的,而是让函数getCustomerProfile()来做所有的工作。我该如何处理这种情况?
发布于 2017-02-15 22:21:59
class MyClass {
String xtype;
String name;
String type;
String value;
constructor(json:any) {
this.xtype = json['@type'];
this.name = json.name;
this.type = json.type;
this.value = json.value;
}
}var myClass = new MyClass(json);https://stackoverflow.com/questions/42251726
复制相似问题