Forum > General

Access violation with JSON

(1/1)

johnmc:
I have a small program that I am writing, it gets some information in JSON format from the web. I want to extract some of this info and put it in a database. So far I can download the data from the web and extract the data to a form. I read somewhere that you should free up any JSON data variables but when I do this it is causing access violations.

Any help would be appreciated.

John


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit Data; {$mode ObjFPC}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, TypInfo,  fpjson, jsonparser; type   { TfrmExtractData }   TfrmExtractData = class(TForm)    btnClose: TButton;    Edit1: TEdit;    Edit2: TEdit;    Edit3: TEdit;    Edit4: TEdit;    Edit5: TEdit;    Edit6: TEdit;    Edit7: TEdit;    GroupBox1: TGroupBox;    Label1: TLabel;    Label2: TLabel;    Label3: TLabel;    Label4: TLabel;    Label5: TLabel;    Label6: TLabel;    Label7: TLabel;    Label8: TLabel;    Label9: TLabel;    Memo1: TMemo;    Memo2: TMemo;    procedure btnCloseClick(Sender: TObject);    procedure FormActivate(Sender: TObject);    //function GetJSONData(JPath: string): string;  private    aEdit: array of TEdit;  public   end; var  frmExtractData: TfrmExtractData; implementation uses  main; {$R *.lfm} { TfrmExtractData } procedure TfrmExtractData.FormActivate(Sender: TObject);var  J, O, P: TJSONData;  A: TJSONArray;  I: integer;begin  J := GetJSON(frmISBN.Memo1.Text);   Edit1.Text := '';  P := J.FindPath('items[0].volumeInfo.title');  if (P <> nil) then Edit1.Text := P.AsString;   Edit2.Text := '';  P := J.FindPath('items[0].volumeInfo.subtitle');  if (P <> nil) then Edit2.Text := P.AsString;   Edit3.Text := '';  P := J.FindPath('items[0].volumeInfo.publisher');  if (P <> nil) then Edit3.Text := P.AsString;   Edit4.Text := '';  P := J.FindPath('items[0].volumeInfo.publishedDate');  if (P <> nil) then Edit4.Text := P.AsString;   Edit5.Text := '';  Edit6.Text := '';   O := J.FindPath('items[0].volumeInfo.industryIdentifiers[0].type');  if (O <> nil) then  begin    P := J.FindPath('items[0].volumeInfo.industryIdentifiers[0].identifier');    case O.AsString of      'ISBN_10':      begin        if (P <> nil) then Edit5.Text := P.AsString;      end;      'ISBN_13':      begin        if (P <> nil) then Edit6.Text := P.AsString;      end;      'Other', 'OTHER':      begin        if (P <> nil) then Edit7.Text := P.AsString;      end;    end;  end;   O := J.FindPath('items[0].volumeInfo.industryIdentifiers[1].type');  if (O <> nil) then  begin    P := J.FindPath('items[0].volumeInfo.industryIdentifiers[1].identifier');    case O.AsString of      'ISBN_10':      begin        if (P <> nil) then Edit5.Text := P.AsString;      end;      'ISBN_13':      begin        if (P <> nil) then Edit6.Text := P.AsString;      end;      'Other', 'OTHER':      begin        if (P <> nil) then Edit7.Text := P.AsString;      end;    end;  end;   Memo1.Text := '';  P := J.FindPath('items[0].volumeInfo.description');  if (P <> nil) then Memo1.Text := P.AsString;   Memo2.Text := '';  P := J.FindPath('items[0].searchInfo.textSnippet');  if (P <> nil) then Memo2.Text := P.AsString;   O := J.FindPath('items[0].volumeInfo.authors');  if (O <> nil) and (O.JSONType = jtArray) then  begin    A := TJSONArray(O);    GroupBox1.Height := (A.Count * 25) + 25;    // Adjust the groupbox size to fit number of edit controls    //ShowMessage('1: Author Count ' + IntToStr(A.Count));    setlength(aEdit, A.Count);  // set the array size to hold the authors edit controls    for I := Low(aEdit) to High(aEdit) do  // for each edit control    begin      aEdit[I] := TEdit.Create(Self);  // create edit control and add to array      with aEdit[I] do      begin        Parent := GroupBox1;    // Define the parent object        Caption := J.FindPath('items[0].volumeInfo.authors[' +          IntToStr(I) + ']').AsString;        // Some text to show        Anchors := [akTop, akLeft, akRight];      // Define which sides will be anchored         AnchorSide[akLeft].Control := Parent;     // Anchor left side to parent        AnchorSide[akLeft].Side := asrLeft;        // Anchor left side of object to left side of parent         AnchorSide[akRight].Control := Parent;    // ditto for right        AnchorSide[akRight].Side := asrRight;         BorderSpacing.Left := 4;        BorderSpacing.Right := 4;        BorderSpacing.Bottom := 2;        BorderSpacing.Top := 2;         if I = 0 then        begin          // for the first edit control we use the groupbox for an anchor          AnchorSide[akTop].Control := Parent;   // anchor top to parent          AnchorSide[akTop].Side := asrTop;      // anchor top to top        end        else        begin          // for all subsequent items we anchor to the item above          AnchorSide[akTop].Control := aEdit[I - 1]; // anchor to edit control above          AnchorSide[akTop].Side := asrBottom;   // top of current to bottom of one above         end;      end;    end;   end;  //ShowMessage('2: The End ');   J.Free;  O.Free;  P.Free;end; procedure TfrmExtractData.btnCloseClick(Sender: TObject);begin  Close;end; end. 

lainz:
Please put a compilable example and include the JSON data.

In any case try to free only this variable J:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  J := GetJSON(frmISBN.Memo1.Text);

Zvoni:
You do realize that TJSONData is an abstract (Base)-Class?
https://www.freepascal.org/docs-html/fcl/fpjson/tjsondata.html

--- Quote ---TJSONData is an abstract class which introduces all properties and methods needed to work with JSON-based data. It should never be instantiated.

--- End quote ---
And...... You try to free something you've never created.

lainz:

--- Quote from: Zvoni on August 09, 2022, 03:20:54 pm ---You do realize that TJSONData is an abstract (Base)-Class?
https://www.freepascal.org/docs-html/fcl/fpjson/tjsondata.html

--- Quote ---TJSONData is an abstract class which introduces all properties and methods needed to work with JSON-based data. It should never be instantiated.

--- End quote ---
And...... You try to free something you've never created.

--- End quote ---

Indeed. Try using JSONObject.

But my advice is correct too, you only need to free the object createad with GetJSON, the others are just pointers that are also free when you free the main thing.

johnmc:
Thanks thats looks like where I'm going wrong.

John

Navigation

[0] Message Index

Go to full version