我有一个名为
我创建了以下形式的类:
我该如何将JSON字符串解析为对象?我做了类似于这样的事情:
location.json
的文件,其中包含以下形式的 JSON 字符串:{
"locations": [
{
"id": 1,
"places": [
{
"id": 1,
"city": "A",
"state": "AB"
}
]
}
}
我创建了以下形式的类:
export class Location{
constructor(public id: number,
public places: Place[],
}
export class Place {
constructor(
public id: number,
public city: string,
public state: string
}
我该如何将JSON字符串解析为对象?我做了类似于这样的事情:
...
export class DashboardComponent {
locations: Locations[];
constructor(private locationService:LocationService) {
this.getLocations()
}
getLocations(){
this.locationService.get('assets/location.json')
.subscribe(res => this.location = res);
}