从Json响应typesciprt访问数组中的值

时间:2017-11-08 11:54:44

标签: json angular typescript

如何在Json响应中访问主题并使用typescript获取类型中的值?

Json回复:

{
"$id": "1",
"Council_ID": 102,
"place": "bla bla bla",
"number": "4644",
"type": 2,
"user_Id": 15,
"subjects": [
    {
        "$id": "2",
        "subjectCode": "464",
        "type": 1,
        "branch": "cairo",
        "gender": true
    },
    {
        "$id": "3",
        "subjectCode": "466",
        "type": 5,
        "branch": "alex",
        "gender": true
    }
],
"absence": []
}

meeting.component.ts:

this.dataStorageService.getCouncilId(this.meetingID).subscribe(response => {
                this.subjectsWithID = response.json();
                console.log(this.subjectsWithID, 'All Response')
                this.typee = this.subjectsWithID.subjects.type;
                console.log(this.typee, 'bla bla');
            });

2 个答案:

答案 0 :(得分:1)

以下是访问值的方法

this.subjectsWithID.Council_ID; // 102
this.subjectsWithID.type; // 2
this.subjectsWithID.$id; // '1'
this.subjectsWithID.subjects[0].$id; // '2'
this.subjectsWithID.subjects[0].type; // 1
this.subjectsWithID.subjects[1].$id; // '3'
this.subjectsWithID.subjects[1].type; // 5

答案 1 :(得分:0)

我可能误解了你的问题。如果是这样,请提供所需输出的一些示例。

this.dataStorageService
    .getCouncilId(this.meetingID)
    .map(json)
    .map(response => response.subjects.map(subject => subject.type))
    .subscribe(response => { ... });