首页 文章

仅使用typescript将JSON数据(而不是 Headers )提取到字符串数组

提问于
浏览
1

我要做的是从JSON中提取数据而不是 Headers (例如,获取1而不是ID或获取foo而不是Name)

[{ID = 1, Name = "foo", Email = "foo@foo.com"},
{ID = 2, Name = "bar", Email = "bar@bar.com"}]

我只想要数据而不是 Headers 的原因是数据可以是动态的 . 在一次调用中,返回的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数据分解为其中的部分以传递到接口,同时区分可能位于同一对象中的不同行?

1 回答

相关问题