Ich baue mir da immer anhand der
API passende Objektstrukturen zusammen. Dann wird der Abruf nicht nur wesentlich simpler, man bekommt auch gleich die Typsicherheit dazu. Ich habe hier nur einen Teil der Felder deklariert. Alle übrigen werden ignoriert. Das lässt sich relativ leicht erweitern.
Delphi-Quellcode:
uses
System.JSON, REST.Json;
type
TArtist = class
private
FName: string;
public
property Name: string read FName;
end;
TAlbum = class
private
FArtists: TArray<TArtist>;
FName: string;
public
property Artists: TArray<TArtist> read FArtists;
property Name: string read FName;
end;
TTrack = class
private
FAlbum: TAlbum;
FArtists: TArray<TArtist>;
FId: string;
FName: string;
public
property Album: TAlbum read FAlbum;
property Artists: TArray<TArtist> read FArtists;
property Id: string read FId;
property Name: string read FName;
end;
TUser = class
private
FDisplay_name: string;
public
property Display_name: string read FDisplay_name;
end;
TPlaylistTrack = class
private
FAdded_at: TDateTime;
FAdded_by: TUser;
FIs_local: Boolean;
FTrack: TTrack;
public
property Added_at: TDateTime read FAdded_at;
property Added_by: TUser read FAdded_by;
property Is_local: Boolean read FIs_local;
property Track: TTrack read FTrack;
end;
TPaging<T: class> = class
private
FHref: string;
FItems: TArray<T>;
FLimit: Integer;
FNext: string;
FOffset: Integer;
FPrevious: string;
FTotal: Integer;
public
property Href: string read FHref;
property Items: TArray<T> read FItems;
property Limit: Integer read FLimit;
property Next: string read FNext;
property Offset: Integer read FOffset;
property Previous: string read FPrevious;
property Total: Integer read FTotal;
end;
TPlayList = class
private
FName: string;
FTracks: TPaging<TPlaylistTrack>;
public
property Name: string read FName;
property Tracks: TPaging<TPlaylistTrack> read FTracks;
end;
Der eigentliche Abrufcode beschränkt auf die im Eingangspost gezeigten Daten sieht dann so aus:
Delphi-Quellcode:
function TSpManager.GetPlaylistTracks(PlaylistID:
string; List: TStrings):
string;
// Liste der Track-IDs aus Playlist holen
var
playlist: TPlaylist;
track: TPlaylistTrack;
begin
result := '
';
RESTRequest1.Resource := '
v1/playlists/{PlaylistID}';
// Accesspoint der Spotify Web-API
RESTRequest1.AddParameter('
PlaylistID', PlaylistID);
RESTRequest1.Execute;
if RestResponse1.JSONValue
is TJSONObject
then begin
playlist := TJson.JsonToObject<TPlaylist>(TJSONObject(RESTResponse1.JSONValue));
if playlist <>
nil then begin
Result := playlist.
Name;
if playlist.Tracks <>
nil then begin
for track
in playlist.Tracks.Items
do begin
if track.Track <>
nil then begin
List.Add(track.Track.Id);
end;
end;
end;
end;
end;
end;