У меня есть API, который я использую в службе Angular 8.0.0. Кажется, я не понимаю, как отобразить json, возвращенный объекту.
Все примеры, которые я видел, были простыми примерами массива четко определенных и легко проходимых json. Набор json, который я получаю, довольно сложный.
В идеале, поскольку там много повторяющихся шаблонов, я бы сделал класс с другими классами в качестве членов класса.
Есть ли способ просто преобразовать возвращаемый наблюдаемый json в соответствующий словарь словарей?
Если бы я шаблонизировал это, я бы столкнулся с тем фактом, что я получаю обратно словарь, а не массив.
Вот шаблон проектирования сервиса, которому я следую. Поскольку этот шаблон проектирования превращает простую полезную нагрузку json в простой интерфейс, я не достаточно сложен, чтобы попытаться транспонировать его в свой вариант использования. У меня есть сомнения относительно возможности преобразования этого сложного набора данных в интерфейс или класс.
getTeams() {
return this.http.get<ITeam[]>(this._url).pipe(
retry(1), catchError(error => {
return throwError(error.message || 'Undefined Server Error');
}));
}
Вот мой примерный набор данных, аналогичный тому, который я пытаюсь использовать в своем угловом компоненте через мой сервис:
{
"stlouis_teams": {
"blues": {
"version": "12.0-57.24",
"image": "stl_team_photo.jpg",
"image_md5": "fffffc429735ffff4d6c716c113f3ffff",
"stadium_phones": {
"landlines": [
"210-555-1212",
"309-555-1212"
],
"cells": [
"312-555-1212",
"502-555-1212"
]
},
"coaching_staff": {
"coach": {
"image": "bill_brasky.jpg",
"image_locations": {
"http": [
"http://example.com/images/bill_brasky.jpg",
"http://example.com/bill_brasky.jpg"
],
"ftp": [
"ftp://example.com/public/images/bill_brasky.jpg",
"ftps://example.com/secure/aoe124/public/images/bill_brasky.jpg"
]
},
"image_md5": "4444444d0a2c1cad1437570f0e4444444"
},
"asst_coach": {
"image": "bill_billy.jpg",
"image_locations": {
"http": [
"http://example.com/images/bill_billy.jpg",
"http://example.com/bill_billy.jpg"
],
"ftp": [
"ftp://example.com/public/images/bill_billy.jpg",
"ftps://example.com/secure/aoe124/public/images/bill_billy.jpg"
]
},
"image_md5": "333333333d0a2c1cad1437570f0e33333333"
}
}
},
"jv_blues": {
"version": "1.0-0.0",
"image": "jv_stl_team_photo.jpg",
"image_md5": "9999c42973599994d6c716c113f39999",
"stadium_phones": {
"landlines": [
"210-555-1111",
"309-555-1111"
],
"cells": [
"312-555-1111",
"502-555-1111"
]
},
"coaching_staff": {
"coach": {
"image": "bill_brasky.jpg",
"image_locations": {
"http": [
"http://example.com/images/bill_brasky.jpg",
"http://example.com/bill_brasky.jpg"
],
"ftp": [
"ftp://example.com/public/images/bill_brasky.jpg",
"ftps://example.com/secure/aoe124/public/images/bill_brasky.jpg"
]
},
"image_md5": "4444444d0a2c1cad1437570f0e4444444"
},
"asst_coach": {
"image": "bill_billy.jpg",
"image_locations": {
"http": [
"http://example.com/images/bill_billy.jpg",
"http://example.com/bill_billy.jpg"
],
"ftp": [
"ftp://example.com/public/images/bill_billy.jpg",
"ftps://example.com/secure/aoe124/public/images/bill_billy.jpg"
]
},
"image_md5": "333333333d0a2c1cad1437570f0e33333333"
}
}
}
},
"chicago_teams": {
"blackhawks": {
"version": "11.33",
"image": "chi_team_photo.jpg",
"image_md5": "00000c42973500004d6c716c113f30000",
"stadium_phones": {
"landlines": [
"702-555-1212",
"412-555-1212"
],
"cells": [
"217-555-1212",
"904-555-1212"
]
},
"coaching_staff": {
"coach": {
"image": "bill_brasky.jpg",
"image_locations": {
"http": [
"http://example.com/images/bill_brasky.jpg",
"http://example.com/bill_brasky.jpg"
],
"ftp": [
"ftp://example.com/public/images/bill_brasky.jpg",
"ftps://example.com/secure/aoe124/public/images/bill_brasky.jpg"
]
},
"image_md5": "4444444d0a2c1cad1437570f0e4444444"
},
"asst_coach": {
"image": "bill_billy.jpg",
"image_locations": {
"http": [
"http://example.com/images/bill_billy.jpg",
"http://example.com/bill_billy.jpg"
],
"ftp": [
"ftp://example.com/public/images/bill_billy.jpg",
"ftps://example.com/secure/aoe124/public/images/bill_billy.jpg"
]
},
"image_md5": "333333333d0a2c1cad1437570f0e33333333"
}
}
},
"minor_blackhawks": {
"version": "1002.2",
"image": "minor_chi_team_photo.jpg",
"image_md5": "ccccccc42973500004d6c716c113fccccc",
"stadium_phones": {
"landlines": [
"702-555-2222",
"412-555-2222"
],
"cells": [
"217-555-2222",
"904-555-2222"
]
},
"coaching_staff": {
"coach": {
"image": "bill_brasky.jpg",
"image_locations": {
"http": [
"http://example.com/images/bill_brasky.jpg",
"http://example.com/bill_brasky.jpg"
],
"ftp": [
"ftp://example.com/public/images/bill_brasky.jpg",
"ftps://example.com/secure/aoe124/public/images/bill_brasky.jpg"
]
},
"image_md5": "4444444d0a2c1cad1437570f0e4444444"
},
"asst_coach": {
"image": "bill_billy.jpg",
"image_locations": {
"http": [
"http://example.com/images/bill_billy.jpg",
"http://example.com/bill_billy.jpg"
],
"ftp": [
"ftp://example.com/public/images/bill_billy.jpg",
"ftps://example.com/secure/aoe124/public/images/bill_billy.jpg"
]
},
"image_md5": "333333333d0a2c1cad1437570f0e33333333"
}
}
}
}
}