Recent

Author Topic: Need help json and tmdb  (Read 325 times)

HengeDK

  • Newbie
  • Posts: 2
Need help json and tmdb
« on: April 18, 2023, 02:29:16 pm »
Need help
I can't figure out why this dosen't work and I get access violation at this line

Code: Pascal  [Select][+][-]
  1. WriteLn('Title: ', Results.Items[i].FindPath('original_name').AsString);

see attachment for json output from tmdb

Code: Pascal  [Select][+][-]
  1. function TFileScanner.SearchMovies(query: string): TJSONArray;
  2. var
  3.   HTTPClient: TFPHTTPClient;
  4.   Response: string;
  5.   JSONData: TJSONData;
  6.   Results: TJSONArray;
  7. begin
  8.   HTTPClient := TFPHTTPClient.Create(nil);
  9.   try
  10.  
  11.     Response := HTTPClient.Get('https://api.themoviedb.org/3/search/tv?api_key=xxxxxxx&query=' + EncodeUrl(query));
  12.   finally
  13.     HTTPClient.Free;
  14.   end;
  15.  
  16.   JSONData := GetJSON(Response);
  17.   try
  18.     Results := JSONData.FindPath('results') as TJSONArray;
  19.   finally
  20.     JSONData.Free;
  21.   end;
  22.  
  23.   //Result := Results;
  24.   WriteLn(Results.ToString);
  25. end;
  26.  


Code: Pascal  [Select][+][-]
  1.   try
  2.     for i := 0 to Results.Count - 1 do
  3.     begin
  4.       WriteLn('Title: ', Results.Items[i].FindPath('original_name').AsString);
  5.       WriteLn('ID: ', Results.Items[i].FindPath('id').AsString);
  6.       WriteLn('Overview: ', Results.Items[i].FindPath('overview').AsString);
  7.       WriteLn;
  8.     end;
  9.   finally
  10.     Results.Free;
  11.   end;
  12.  


avk

  • Hero Member
  • *****
  • Posts: 744
Re: Need help json and tmdb
« Reply #1 on: April 18, 2023, 07:41:19 pm »
In the FpJson implementation, a JSON document is a rooted tree, where each element is responsible for freeing the memory occupied by its children.

Respectively, by calling Free() for the root element (JSONData.Free, line 20) you thus destroy the entire document, including the Results element.

HengeDK

  • Newbie
  • Posts: 2
Re: Need help json and tmdb
« Reply #2 on: April 19, 2023, 11:16:54 am »
Thanks  :)

 

TinyPortal © 2005-2018