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.