【发布时间】:2017-12-10 15:16:36
【问题描述】:
我要做的是仅从 JSON 中提取数据而不是标头(例如,获取 1 但不获取 ID 或获取 foo 而不是名称)
[{ID = 1, Name = "foo", Email = "foo@foo.com"},
{ID = 2, Name = "bar", Email = "bar@bar.com"}]
我只想要数据而不是标题的原因是数据可以是动态的。在一次调用中,返回的 JSON 可能每个对象有 100 个字段,或者在下一次调用中每个对象有 2 个字段。这就是为什么在下面的示例中,我的界面中只有一个字符串,因为我不知道可以传递什么样的数据。
这是我试图解释数据的打字稿
import { Component } from '@angular/core';
import { Http } from '@angular/http';
@Component({
selector: 'fetchdata',
template: require('./fetchdata.component.html')
})
export class FetchDataComponent {
public rowData: RowInfo[];
constructor(http: Http) {
http.get('/api/SampleData/DatatableData').subscribe(result => {
//This is where the magic should happen
//This currently does not work
var dataCarrier = result.toString();
JSON.parse(dataCarrier).forEach(item => {
this.rowData = item.name;
});
});
}
}
interface RowInfo {
rowData: string;
}
如何将 http.get 中的 JSON 数据分解成几部分以传递到接口,同时区分同一对象中可能存在的不同行?
【问题讨论】:
标签: javascript arrays json angular typescript