【发布时间】:2016-04-02 02:09:16
【问题描述】:
我正在使用一个使用 XML 而不是 JSON 的 API。有关如何将以下 XML 转换为 JSON 或如何正确使用 ngFor 指令中的数据的任何建议?
另外,observable 在这里合适吗?
<case-file>
<serial-number>123456789</serial-number>
<transaction-date>20150101</transaction-date>
<case-file-header>
<filing-date>20140101</filing-date>
</case-file-header>
// ...
<classifications>
<classification>
<international-code-total-no>1</international-code-total-no>
<primary-code>025</primary-code>
</classification>
</classifications>
</case-file>
<case-file>
<serial-number>234567890</serial-number>
<transaction-date>20160401</transaction-date>
<case-file-header>
<filing-date>20160401</filing-date>
</case-file-header>
//...
<classifications>
<classification>
<international-code-total-no>1</international-code-total-no>
<primary-code>042</primary-code>
</classification>
</classifications>
</case-file>
export class apiService {
constructor (private http: Http) {}
private _apiUrl = 'app/api';
getCaseFile () {
return this.http.get(this._apiUrl)
//conversion to JSON here?
.map(res => <CaseFile[]> res.json().data)
.catch(this.handleError);
}
private handleError (error: Response) {
console.error(error);
return Observable.throw(error.json().error || 'Server error');
}
}
<div *ngFor="#cf of case-file">{{case-file.serial-number}}</div>
【问题讨论】:
-
那里有几个 xml 到 json 库
-
@juvian 我欢迎推荐。我不知道 Angular 2 有什么。
-
不需要angular2,它与组件或任何角度相关的东西都没有关系,只需将xml转换为json,然后使用angular:stackoverflow.com/questions/1773550/…
标签: xml angular typescript observable