将复杂的Observable json映射到类或接口

时间:2019-05-31 03:26:26

标签: json angular

我有一个Angular 8.0.0服务正在使用的API。我似乎无法理解如何将返回的json映射到对象。

我看到的所有示例都是简单定义的数组,这些数组包含定义明确且易于遍历的json。我要返回的json集非常复杂。

理想情况下,由于其中存在很多重复的模式,因此我将创建一个包含其他类的类作为该类的成员。

  1. 有没有一种方法可以将返回的可观察json转换为相关的字典词典?

  2. 如果要对此进行模板化,那么我将无法返回字典而不是数组。

这是我遵循的服务设计模式。由于此设计模式将一个简单的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"
                }
            }
        }
    }
}

0 个答案:

没有答案