[英]How to map json array to angular model in map operator
I am trying to map my JSON array objects from my server to an angular model. 我正在尝试将我的JSON数组对象从服务器映射到角度模型。 I believe the best place to do this is as soon as I have them in the pipe at map.
我相信最好的方法就是尽快将它们放入地图管道中。 Not sure how I can do this with the map operator.
不确定如何使用地图运算符执行此操作。
Later on I use a complex data source process to fill my table and paginate. 稍后,我将使用复杂的数据源流程填充表格并进行分页。 I do not see how I could break up the json after this initial get from the server.
我看不到如何从服务器初始获取后分解json。
I have tried using classes and constructors. 我试过使用类和构造函数。 I have tried creating another class that takes the list of that particular model.
我尝试创建另一个采用该特定模型列表的类。 And I have tried using a deserilizable method on the class.
而且我尝试在类上使用可反序列化的方法。 I believe there must be an angular magic way of doing this as interfaces.
我相信必须有一种角度的魔术方法可以做到这一点。 Any ideas?
有任何想法吗?
ps Still new to reactive programming! ps对于反应式编程来说还是新手!
export class PlaylistService {
constructor(private http: HttpClient) { }
findPlaylistTracks(
playlistId: string, filter = '', sortOrder = 'asc',
pageNumber = 0, pageSize = 3): Observable<PlaylistItem[]> {
return this.http.get<PlaylistItem[]>('api/spotify/playlist- item',{
params: new HttpParams()
.set('playlistId', playlistId.toString())
.set('filter', filter)
.set('sortOrder', sortOrder)
.set('pageNumber', pageNumber.toString())
.set('pageSize', pageSize.toString())
})
.pipe(
map((res: PlaylistItem[]) => <PlaylistItem[]>res['payload']),
tap( val => console.log('are they mapped to models ?',val)));
}
}
export interface PlaylistItem {
albumInfo: PlayListAlbum;
artists: PlayListArtist;
playlistName: string;
trackNumber: number;
trackDuration: string;
trackUri: string;
}
export interface PlayListArtist{
artistName: string;
fullArtistInfo: string;
}
export interface PlayListAlbum{
albumName: string;
relatedAlbums: string;
}
{playlist_name: "Dubliners", track_name: "Seven Drunken Nights - 1993
Remaster", album_info: {…}, artists: {…}, track_duration: "3:46", …}
{playlist_name: "Dubliners", track_name: "The Black Velvet Band",
album_info: {…}, artists: {…}, track_duration: "3:36", …}
{playlist_name: "Dubliners", track_name: "The Foggy Dew", album_info:
{…}, artists: {…}, track_duration: "3:42", …}
No errors. 没有错误。 The val in the tap() log still prints the json key/values.
tap()日志中的val仍会打印json键/值。
This is what I did in the end: 这是我最后所做的:
created this interface. 创建了此接口。
export interface Deserializable {
deserialize(input: any): this;
}
added it to my classes 将其添加到我的班级
export class PlaylistItem implements Deserializable {
public album_info: PlayListAlbum;
public artists: PlayListArtist;
public playlist_name: string;
public track_number: number;
public track_duration: string;
public track_uri: string;
public track_name: string;
deserialize(input: any) {
Object.assign(this, input);
this.album_info = new PlayListAlbum().deserialize(input.album_info);
this.artists = new PlayListArtist().deserialize(input.artists);
return this;
}
}
export class PlayListAlbum implements Deserializable {
public album_name: string;
public related_albums: string;
deserialize(input: any): this {
return Object.assign(this, input);
}
}
export class PlayListArtist implements Deserializable {
public artist_name: string;
public full_artist_info: string;
deserialize(input: any): this {
return Object.assign(this, input);
}
}
Then did the conversion in Pipe() -> Map() like so: 然后在Pipe()-> Map()中进行转换,如下所示:
}).pipe(
map(res => res['payload'].map(data => new PlaylistItem().deserialize(data)))
);
}
I have to use snake case like the json. 我必须使用像json这样的蛇形盒。 But it works.
但这有效。 Now I have angular models.
现在我有了角度模型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.