As a novice in dealing with json objects, I am having trouble extracting data from the GroupResult object. Below is the structure of my classes:
export class GroupResult {
Id!: number;
Lecturer!: string;
GroupName!: string;
Subjects!: SubjectStatsStudent[];
}
export class SubjectStatsStudent {
Id!: number;
Name!: string;
StudentResults!: StudentResult[];
}
export class StudentResult {
Id!: number;
Name!: string;
Number!: number;
LabPass!: number;
LetionPass!: number;
AllPass!: number;
LabAvg!: number;
TestAvg!: number;
Rating!: number;
}
I attempted to extract the data in the following manner:
(service.ts)
import {HttpClient} from '@angular/common/http';
import {Injectable} from '@angular/core';
import { Observable } from 'rxjs';
import { GroupResult } from '../model/group.results';
@Injectable({
providedIn: 'root'
})
export class GroupService {
getTest(): Observable<GroupResult> {
return this.http.get<GroupResult>('../../assets/testGroup.json');
}
}
(group.component.ts)
ngOnInit(): void {
this.groupService.getTest().subscribe((res: GroupResult) => this.Group = res);
this.SelectedSubject = this.Group.Subjects[0];
}
(testGroup.json)
{
"Id": "777",
"Lecturer": "Ivanov",
"GroupName": "10701218",
"Subjects": [
{
"Id": "5",
"Name": "five",
"StudentResults": [
{
"Id": "5",
"Name": "five",
"Number": "1",
"LabPass": "1",
"LectionPass": "1",
"AllPass": "1",
"LabAvg": "1",
"TestAvg": "1",
"Rating": "1"
},
{
"Id": "3",
"Name": "two",
"Number": "2",
"LabPass": "2",
"LectionPass": "2",
"AllPass": "2",
"LabAvg": "2",
"TestAvg": "2",
"Rating": "2"
},
{
"Id": "7",
"Name": "tree",
"Number": "3",
"LabPass": "3",
"LectionPass": "3",
"AllPass": "3",
"LabAvg": "3",
"TestAvg": "3",
"Rating": "3"
}
]
},
{
"Id": "7",
"Name": "f77777e",
"StudentResults": [
{
"Id": "5",
"Name": "five",
"Number": "1",
"LabPass": "1",
"LectionPass": "1",
"AllPass": "1",
"LabAvg": "1",
"TestAvg": "1",
"Rating": "1"
},
{
"Id": "3",
"Name": "two",
"Number": "2",
"LabPass": "2",
"LectionPass": "2",
"AllPass": "2",
"LabAvg": "2",
"TestAvg": "2",
"Rating": "2"
},
{
"Id": "7",
"Name": "tree",
"Number": "3",
"LabPass": "3",
"LectionPass": "3",
"AllPass": "3",
"LabAvg": "3",
"TestAvg": "3",
"Rating": "3"
}
]
},
{
"Id": "3",
"Name": "fiv333e",
"StudentResults": [
{
"Id": "5",
"Name": "five",
"Number": "1",
"LabPass": "1",
"LectionPass": "1",
"AllPass": "1",
"LabAvg": "1",
"TestAvg": "1",
"Rating": "1"
},
{
"Id": "3",
"Name": "two",
"Number": "2",
"LabPass": "2",
"LectionPass": "2",
"AllPass": "2",
"LabAvg": "2",
"TestAvg": "2",
"Rating": "2"
},
{
"Id": "7",
"Name": "tree",
"Number": "3",
"LabPass": "3",
"LectionPass": "3",
"AllPass": "3",
"LabAvg": "3",
"TestAvg": "3",
"Rating": "3"
}
]
}
]
}
However, I encountered the error:
"Cannot read property 'Subjects' of undefined"
. It seems that my Group variable is still undefined. Can someone please guide me on what mistake I might be making?