Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- Autor: Adriano Schneider
- Data: 2017-08
- }
- unit ufrmPrincipal;
- interface
- uses
- System.SysUtils,
- System.Types,
- System.UITypes,
- System.Variants,
- System.Rtti,
- System.JSON,
- IPPeerClient,
- FMX.Types,
- FMX.Controls,
- FMX.Forms,
- FMX.Graphics,
- FMX.Dialogs,
- FMX.TabControl,
- FMX.Layouts,
- FMX.Controls.Presentation,
- FMX.StdCtrls,
- FMX.Objects,
- FMX.ScrollBox,
- FMX.Memo,
- FMX.ComboEdit,
- FMX.Edit,
- FMX.Grid.Style,
- FMX.Grid,
- FMX.ListView.Types,
- FMX.ListView.Appearances,
- FMX.ListView.Adapters.Base,
- FMX.ListView,
- REST.Client,
- REST.Types,
- System.JSON.Builders,
- System.JSON.Readers,
- Data.Bind.Components,
- Data.Bind.ObjectScope,
- FireDAC.Stan.Intf,
- FireDAC.Stan.Option,
- FireDAC.Stan.Param,
- FireDAC.Stan.Error,
- FireDAC.DatS,
- FireDAC.Phys.Intf,
- FireDAC.DApt.Intf,
- Datasnap.DBClient,
- Data.DB,
- FireDAC.Comp.DataSet,
- FireDAC.Comp.Client,
- System.Classes,
- System.Generics.Collections,
- System.Math.Vectors;
- type
- TfrmPrincipal = class(TForm)
- Layout1: TLayout;
- Layout2: TLayout;
- Layout3: TLayout;
- TabControl: TTabControl;
- tabRegister: TTabItem;
- tabLogin: TTabItem;
- Layout4: TLayout;
- Layout5: TLayout;
- Label1: TLabel;
- pnlTitulo: TRectangle;
- lblTitulo: TLabel;
- SpeedButton1: TSpeedButton;
- tabList: TTabItem;
- tabGet: TTabItem;
- tabUpdate: TTabItem;
- Layout6: TLayout;
- Layout7: TLayout;
- tabDelete: TTabItem;
- TabItem7: TTabItem;
- Layout8: TLayout;
- Layout9: TLayout;
- Layout10: TLayout;
- Layout11: TLayout;
- Memo: TMemo;
- Layout12: TLayout;
- edtAPIKEY: TEdit;
- edtURL: TComboEdit;
- Rectangle1: TRectangle;
- ListView1: TListView;
- Splitter1: TSplitter;
- Layout13: TLayout;
- Label7: TLabel;
- edtRegistrarNome: TEdit;
- Layout14: TLayout;
- Label8: TLabel;
- edtRegistrarEmail: TEdit;
- Layout15: TLayout;
- Label9: TLabel;
- edtRegistrarSenha: TEdit;
- Layout16: TLayout;
- Label2: TLabel;
- edtEmail: TEdit;
- Label10: TLabel;
- Layout17: TLayout;
- Label11: TLabel;
- edtPassword: TEdit;
- Layout18: TLayout;
- Label12: TLabel;
- edtRegistrarCelular: TEdit;
- Label13: TLabel;
- Label3: TLabel;
- Label4: TLabel;
- Label5: TLabel;
- tabCreate: TTabItem;
- Layout19: TLayout;
- Label6: TLabel;
- Layout20: TLayout;
- Label14: TLabel;
- edtRegistrarReSenha: TEdit;
- Layout21: TLayout;
- Label15: TLabel;
- Edit8: TEdit;
- Layout22: TLayout;
- Label16: TLabel;
- btnLogin: TButton;
- Layout23: TLayout;
- Label17: TLabel;
- btnRegistrar: TButton;
- Layout24: TLayout;
- Label18: TLabel;
- Button3: TButton;
- Layout25: TLayout;
- Label19: TLabel;
- Edit9: TEdit;
- Layout26: TLayout;
- Label20: TLabel;
- Button4: TButton;
- Layout27: TLayout;
- Label21: TLabel;
- Edit10: TEdit;
- Layout28: TLayout;
- Label22: TLabel;
- Button5: TButton;
- Layout29: TLayout;
- Label23: TLabel;
- Edit11: TEdit;
- RESTClient: TRESTClient;
- RESTRequest: TRESTRequest;
- Layout30: TLayout;
- Label24: TLabel;
- lblError: TLabel;
- Layout31: TLayout;
- Label25: TLabel;
- lblRegistrarError: TLabel;
- Layout32: TLayout;
- Button1: TButton;
- Layout33: TLayout;
- Label26: TLabel;
- Button2: TButton;
- edtID: TEdit;
- btnVaiRegister: TButton;
- Layout34: TLayout;
- Label27: TLabel;
- Button9: TButton;
- Button6: TButton;
- Layout35: TLayout;
- Label28: TLabel;
- Edit1: TEdit;
- Layout36: TLayout;
- Label29: TLabel;
- Edit2: TEdit;
- Layout37: TLayout;
- Label30: TLabel;
- Edit3: TEdit;
- Layout38: TLayout;
- Label31: TLabel;
- Edit4: TEdit;
- StringGrid: TStringGrid;
- tabTeste: TTabItem;
- Button7: TButton;
- Memo2: TMemo;
- Memo1: TMemo;
- Button8: TButton;
- Button10: TButton;
- Layout39: TLayout;
- Button11: TButton;
- Splitter2: TSplitter;
- tabTeste2: TTabItem;
- Layout41: TLayout;
- btnSave: TButton;
- Button22: TButton;
- Memo3: TMemo;
- Splitter3: TSplitter;
- TabItem1: TTabItem;
- Layout42: TLayout;
- Button21: TButton;
- Button23: TButton;
- Splitter4: TSplitter;
- Memo4: TMemo;
- TabItem2: TTabItem;
- Layout43: TLayout;
- Splitter5: TSplitter;
- Memo5: TMemo;
- StrGrdCsv: TStringGrid;
- StringColumn1: TStringColumn;
- StringColumn2: TStringColumn;
- StringColumn3: TStringColumn;
- StringColumn4: TStringColumn;
- StringColumn5: TStringColumn;
- FDMemTable1: TFDMemTable;
- ClientDataSet1: TClientDataSet;
- DataSource1: TDataSource;
- Grid1: TGrid;
- Button12: TButton;
- Layout40: TLayout;
- Layout44: TLayout;
- Label32: TLabel;
- edtNome: TEdit;
- Layout45: TLayout;
- Label33: TLabel;
- Layout46: TLayout;
- Label34: TLabel;
- edtIdade: TEdit;
- Layout48: TLayout;
- Label36: TLabel;
- Layout49: TLayout;
- RadioButton1: TRadioButton;
- RadioButton2: TRadioButton;
- btnObjectToJson: TButton;
- btnJsonToObject: TButton;
- Button13: TButton;
- procedure SpeedButton1Click(Sender: TObject);
- procedure FormActivate(Sender: TObject);
- procedure btnLoginClick(Sender: TObject);
- procedure btnRegistrarClick(Sender: TObject);
- procedure Button1Click(Sender: TObject);
- procedure Button3Click(Sender: TObject);
- procedure Button2Click(Sender: TObject);
- procedure Button4Click(Sender: TObject);
- procedure Button5Click(Sender: TObject);
- procedure btnVaiRegisterClick(Sender: TObject);
- procedure Button9Click(Sender: TObject);
- procedure Button6Click(Sender: TObject);
- procedure Button7Click(Sender: TObject);
- procedure Button8Click(Sender: TObject);
- procedure Button10Click(Sender: TObject);
- procedure Button11Click(Sender: TObject);
- procedure btnSaveClick(Sender: TObject);
- procedure Button21Click(Sender: TObject);
- procedure btnObjectToJsonClick(Sender: TObject);
- procedure btnJsonToObjectClick(Sender: TObject);
- procedure FormClose(Sender: TObject; var Action: TCloseAction);
- procedure Button13Click(Sender: TObject);
- private
- procedure ParseObject;
- procedure JsonToDataset(aDataset: TDataSet; aJSON: string);
- function JsonListObjeto(jsonString: String; classe: TClass): TList<TObject>;
- function JsonListObjeto1(valor: String; classe: TClass): TList<TObject>;
- function DataSetToJsonTxt(pDataSet: TDataSet): string;
- { Private declarations }
- public
- { Public declarations }
- end;
- Type
- TPessoa = class
- strict private
- FNome:String;
- FIdade:integer;
- FEstadoCivil:Boolean;
- public
- property Nome:String read FNome write FNome;
- property Idade:integer read FIdade write FIdade;
- property EstadoCivil:Boolean read FEstadoCivil write FEstadoCivil;
- end;
- type
- THelper = record
- end;
- type
- TCliente = record
- end;
- var
- frmPrincipal: TfrmPrincipal;
- objValueJson:TJSONValue;
- FobjValueJson:TJSONValue;
- implementation
- {$R *.fmx}
- uses
- REST.Response.Adapter,
- DBXJSon,
- DBXJsonReflect;
- procedure TfrmPrincipal.btnLoginClick(Sender: TObject);
- var
- vJSON: string;
- valJSON: TJSONValue;
- begin
- RESTClient.ContentType := 'application/x-www-form-urlencoded';
- RESTClient.BaseURL := edtURL.text+'/login';
- RESTRequest.Method := TRESTRequestMethod.rmPOST;
- RESTRequest.Params.Clear;
- RESTRequest.AddParameter(
- {AName} 'email',
- {AValue} edtEmail.Text,
- {AKind} TRESTRequestParameterKind.pkGETorPOST
- );
- RESTRequest.AddParameter(
- {AName} 'password',
- {AValue} edtPassword.Text,
- {AKind} TRESTRequestParameterKind.pkGETorPOST
- );
- RESTRequest.SynchronizedEvents := False;
- RESTRequest.Execute;
- Memo.Lines.Clear;
- Memo.Lines.Add(RESTClient.BaseURL);
- vJSON := RESTRequest.Response.JSONText;
- valJSON := TJSONObject.ParseJSONValue(vJSON);
- if (valJSON.GetValue<string>('error')='false') then
- begin
- edtAPIKEY.Text := valJSON.GetValue<string>('apiKey');
- lblError.Text := '';
- end
- else
- begin
- edtAPIKEY.Text := '';
- lblError.Text := valJSON.GetValue<string>('message');;
- end;
- Memo.Lines.Add(vJSON);
- TabControl.ActiveTab := tabList;
- end;
- procedure TfrmPrincipal.btnRegistrarClick(Sender: TObject);
- var
- vJSON: string;
- valJSON: TJSONValue;
- begin
- if (edtRegistrarSenha.Text<>edtRegistrarReSenha.Text) then
- begin
- lblRegistrarError.Text := 'A digitação da senha não coincidiu!';
- edtRegistrarSenha.SetFocus;
- end
- else
- begin
- lblRegistrarError.Text := '';
- RESTClient.ContentType := 'application/x-www-form-urlencoded';
- RESTClient.BaseURL := edtURL.text+'/register';
- RESTRequest.Method := TRESTRequestMethod.rmPOST;
- RESTRequest.Params.Clear;
- RESTRequest.AddParameter(
- {AName} 'name',
- {AValue} edtRegistrarNome.Text,
- {AKind} TRESTRequestParameterKind.pkGETorPOST
- );
- RESTRequest.AddParameter(
- {AName} 'celular',
- {AValue} edtRegistrarCelular.Text,
- {AKind} TRESTRequestParameterKind.pkGETorPOST
- );
- RESTRequest.AddParameter(
- {AName} 'email',
- {AValue} edtRegistrarEmail.Text,
- {AKind} TRESTRequestParameterKind.pkGETorPOST
- );
- RESTRequest.AddParameter(
- {AName} 'password',
- {AValue} edtRegistrarSenha.Text,
- {AKind} TRESTRequestParameterKind.pkGETorPOST
- );
- RESTRequest.SynchronizedEvents := False;
- RESTRequest.Execute;
- Memo.Lines.Clear;
- Memo.Lines.Add(RESTClient.BaseURL);
- vJSON := RESTRequest.Response.JSONText;
- valJSON := TJSONObject.ParseJSONValue(vJSON);
- if (valJSON.GetValue<string>('error')='false') then
- begin
- lblRegistrarError.Text := '';
- end
- else
- begin
- lblRegistrarError.Text := valJSON.GetValue<string>('message');;
- end;
- Memo.Lines.Add(vJSON);
- end;
- end;
- procedure TfrmPrincipal.btnVaiRegisterClick(Sender: TObject);
- begin
- TabControl.ActiveTab := tabRegister;
- end;
- procedure TfrmPrincipal.Button10Click(Sender: TObject);
- var
- o: TJSONObject;
- a: TJSONArray;
- book: TJSONObject;
- idx: integer;
- idy: integer;
- begin
- o := TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(Memo1.Lines.Text),0) as TJSONObject;
- try
- a := TJSONArray(o.Get('books').JsonValue);
- for idx := 0 to pred(a.size) do begin
- book := TJSONObject(a.Get(idx));
- for idy := 0 to pred(book.Count) do begin
- ShowMessage( book.Pairs[idy].JsonString.ToString + ':' + book.Pairs[idy].JsonValue.ToString );
- end;
- end;
- finally
- o.Free;
- end;
- end;
- procedure TfrmPrincipal.Button11Click(Sender: TObject);
- var
- oJSON: TJSONObject;
- aJSON: TJSONArray;
- oRecordJSON: TJSONObject;
- idx: integer;
- idy: integer;
- begin
- oJSON := TJSONObject.ParseJSONValue(
- TEncoding.ASCII.GetBytes(Memo2.Lines.Text),0) as TJSONObject;
- ShowMessage(oJSON.GetValue('total').ToString);
- try
- aJSON := TJSONArray(oJSON.Get('registros').JsonValue);
- for idx := 0 to pred(aJSON.size) do begin
- oRecordJSON := TJSONObject(aJSON.Get(idx));
- for idy := 0 to pred(oRecordJSON.Count) do begin
- ShowMessage(oRecordJSON.Pairs[idy].JsonString.ToString + ':' +
- oRecordJSON.Pairs[idy].JsonValue.ToString);
- end;
- end;
- finally
- oJSON.Free;
- end;
- end;
- function TfrmPrincipal.JsonListObjeto(jsonString: String; classe: TClass): TList<TObject>;
- var lista:Tlist<TObject>;
- jo: TJSONObject;
- obj: TObject;
- jl: TJSONArray;
- i:integer;
- unMarshal: TJSONUnMarshal;
- //xxxxx
- begin
- lista := TList<TObject>.Create;
- jo := TJSONObject.Create;
- obj := TObject.Create;
- jl := TJSONArray.Create;
- unMarshal := TJSONUnMarshal.Create;
- jl := TJSONObject.ParseJSONValue(jsonString) as TJsonArray;
- if Assigned(jl) then
- begin
- for i := 0 to jl.Count -1 do
- begin
- jo := jl.Items[i] as TJSONObject;
- //Verificar como funcionava...
- //obj := unMarshal.CreateObject(classe,jo);
- lista.Add(obj);
- end;
- end;
- result := lista;
- end;
- procedure TfrmPrincipal.Button13Click(Sender: TObject);
- begin
- Memo5.Lines.Text := DataSetToJsonTxt(ClientDataSet1);//(PasseAquiSeuDataSet)
- end;
- function TfrmPrincipal.JsonListObjeto1(valor: String; classe: TClass): TList<TObject>;
- var
- lista:Tlist<TObject>;
- jo: TJSONObject;
- obj: TObject;
- jl: TJSONArray;
- i:integer;
- unMarshal: TJSONUnMarshal;
- begin
- lista := TList<TObject>.Create;
- jo := TJSONObject.Create;
- obj := TObject.Create;
- jl := TJSONArray.Create;
- unMarshal := TJSONUnMarshal.Create;
- jl := TJSONObject.ParseJSONValue(valor) as TJsonArray;
- for i := 0 to jl.Count -1 do
- begin
- jo := jl.Items[i] as TJSONObject;
- //obj := unMarshal.CreateObject(classe,jo);
- lista.Add(obj);
- end;
- result := lista;
- end;
- procedure TfrmPrincipal.btnObjectToJsonClick(Sender: TObject);
- var
- M: TJSONMarshal;
- pessoa: TPessoa;
- begin
- pessoa := TPessoa.Create;
- try
- pessoa.Nome := edtNome.Text;
- pessoa.Idade := StrToIntDef(edtIdade.Text, 0);
- //case rdgEstadoCivil.ItemIndex of
- // 0: pessoa.EstadoCivil := true;
- // 1: pessoa.EstadoCivil := false
- //end;
- M := TJSONMarshal.Create(TJSONConverter.Create);
- try
- FobjValueJson.Free;
- FobjValueJson := M.Marshal(pessoa);
- Memo5.Lines.text := FobjValueJson.ToString;
- finally
- M.Free;
- end;
- finally
- pessoa.Free;
- end;
- end;
- procedure TfrmPrincipal.btnJsonToObjectClick(Sender: TObject);
- var
- unM: TJSONUnMarshal;
- pessoa: TPessoa;
- begin
- if not Assigned(FobjValueJson) then
- begin
- exit();
- end;
- unM := TJSONUnMarshal.Create;
- pessoa := TPessoa(unM.Unmarshal(FObjValueJSon));
- edtNome.Text := pessoa.Nome;
- edtIdade.Text := IntToStr(pessoa.Idade);
- //case integer(pessoa.EstadoCivil) of
- // 0: rdgEstadoCivil.ItemIndex := 0;
- // 1: rdgEstadoCivil.ItemIndex := 1;
- //end;
- Memo5.Text := FobjValueJson.ToString;
- unM.Free;
- pessoa.Free;
- end;
- procedure TfrmPrincipal.Button1Click(Sender: TObject);
- begin
- RESTClient.Params.AddHeader('Authorization', edtAPIKEY.Text);
- RESTClient.BaseURL := edtURL.text+'/rfid';
- RESTRequest.Method := TRESTRequestMethod.rmGET;
- RESTRequest.Execute;
- Memo.Lines.Clear;
- Memo.Lines.Add(RESTRequest.Response.JSONText);
- end;
- procedure TfrmPrincipal.btnSaveClick(Sender: TObject);
- var
- i: Integer;
- lJsonObj: TJSONObject;
- begin
- lJsonObj := TJSONObject.Create;
- if btnSave.Text = 'Salvar' then
- begin
- for i := 0 to StrGrdCsv.ColumnCount do
- begin
- lJsonObj.AddPair(StrGrdCsv.Cells[i, 0], StrGrdCsv.Cells[i, 1]);
- end;
- //TFile.WriteAllText(cmbBXDBTables.Text + '.json', lJsonObj.ToString);
- btnSave.Text := 'Load Data';
- end;
- end;
- procedure TfrmPrincipal.JsonToDataset(aDataset : TDataSet; aJSON : string);
- var
- JObj: TJSONArray;
- vConv : TCustomJSONDataSetAdapter;
- begin
- if (aJSON = EmptyStr) then
- begin
- Exit;
- end;
- JObj := TJSONObject.ParseJSONValue(aJSON) as TJSONArray;
- vConv := TCustomJSONDataSetAdapter.Create(Nil);
- try
- vConv.Dataset := aDataset;
- vConv.UpdateDataSet(JObj);
- finally
- vConv.Free;
- JObj.Free;
- end;
- end;
- procedure TfrmPrincipal.Button21Click(Sender: TObject);
- var
- sJSON : string;
- begin
- sJSON := Memo4.Text;
- JsonToDataset(FDMemTable1, sJSON);
- JsonToDataset(ClientDataSet1, sJSON);
- end;
- procedure TfrmPrincipal.Button2Click(Sender: TObject);
- begin
- RESTClient.Params.AddHeader('Authorization', edtAPIKEY.Text);
- RESTClient.BaseURL := edtURL.text+'/rfid/'+edtID.Text;
- RESTRequest.Method := TRESTRequestMethod.rmGET;
- RESTRequest.Params.Clear;
- RESTRequest.Execute;
- Memo.Lines.Clear;
- Memo.Lines.Add(RESTRequest.Response.JSONText);
- end;
- procedure TfrmPrincipal.Button3Click(Sender: TObject);
- begin
- RESTClient.ContentType := 'application/x-www-form-urlencoded';
- RESTClient.Params.AddHeader('Authorization', edtAPIKEY.Text);
- RESTClient.BaseURL := edtURL.text+'/rfid';
- RESTRequest.Method := TRESTRequestMethod.rmPOST;
- RESTRequest.Params.Clear;
- RESTRequest.AddParameter(
- {AName} 'task',
- {AValue} 'Tarefa nro: '+FormatDateTime('dd/mm/yy hh:nn:ss zzz', Now),
- {AKind} TRESTRequestParameterKind.pkGETorPOST
- );
- RESTRequest.SynchronizedEvents := False;
- RESTRequest.Execute;
- Memo.Lines.Clear;
- Memo.Lines.Add(RESTRequest.Response.JSONText);
- end;
- procedure TfrmPrincipal.Button4Click(Sender: TObject);
- begin
- RESTClient.ContentType := 'application/x-www-form-urlencoded';
- RESTClient.Params.AddHeader('Authorization', edtAPIKEY.Text);
- RESTClient.BaseURL := edtURL.text+'/rfid/'+edtID.Text;
- RESTRequest.Method := TRESTRequestMethod.rmPUT;
- RESTRequest.Params.Clear;
- RESTRequest.AddParameter(
- {AName} 'task',
- {AValue} 'Tarefa '+FormatDateTime('dd/mm/yyyy hh:nn:ss zzz', now)+' alterada',
- {AKind} TRESTRequestParameterKind.pkGETorPOST
- );
- RESTRequest.AddParameter(
- {AName} 'status',
- {AValue} '2',
- {AKind} TRESTRequestParameterKind.pkGETorPOST
- );
- RESTRequest.SynchronizedEvents := False;
- RESTRequest.Execute;
- Memo.Lines.Clear;
- Memo.Lines.Add(RESTRequest.Response.JSONText);
- end;
- procedure TfrmPrincipal.Button5Click(Sender: TObject);
- begin
- RESTClient.ContentType := 'application/x-www-form-urlencoded';
- RESTClient.Params.AddHeader('Authorization', edtAPIKEY.Text);
- RESTClient.BaseURL := edtURL.text+'/rfid/'+edtID.Text;
- RESTRequest.Method := TRESTRequestMethod.rmDELETE;
- RESTRequest.Params.Clear;
- RESTRequest.SynchronizedEvents := False;
- RESTRequest.Execute;
- Memo.Lines.Clear;
- Memo.Lines.Add(RESTRequest.Response.JSONText);
- end;
- procedure TfrmPrincipal.Button6Click(Sender: TObject);
- begin
- TabControl.ActiveTab := tabLogin;
- end;
- procedure TfrmPrincipal.Button7Click(Sender: TObject);
- begin
- ParseObject;
- end;
- procedure TfrmPrincipal.Button8Click(Sender: TObject);
- var
- o: TJSONObject;
- a: TJSONArray;
- book: TJSONObject;
- begin
- // Create the outer JSON object which parents the others.
- o := TJSONObject.Create;
- try
- // Create the books object, which contains the array of books...
- a := TJSONArray.Create();
- // add the array to the object.
- o.AddPair('books',a);
- // Create the first book
- book := TJSONObject.Create;
- book.AddPair( TJSONPair.Create('title','Zen') );
- book.AddPair( TJSONPair.Create('subtitle','and The art of motorcycle maintenance.') );
- book.AddPair( TJSONPair.Create('author','Robert M Pirsig') );
- book.AddPair( TJSONPair.Create('isbn','9780061908019') );
- // Add the book to the array
- a.AddElement(book);
- // Create the second book
- book := TJSONObject.Create;
- book.AddPair( TJSONPair.Create('title','Coding in Delphi') );
- book.AddPair( TJSONPair.Create('subtitle','') );
- book.AddPair( TJSONPair.Create('author','Nick Hodges') );
- book.AddPair( TJSONPair.Create('isbn','978-1941266038') );
- // Add the book to the array
- a.AddElement(book);
- finally
- Memo1.Lines.Text := o.ToString;
- o.Free;
- end;
- end;
- Procedure TfrmPrincipal.ParseObject;
- var
- LIterator: TJSONIterator;
- LJsonTextReader: TJsonTextReader;
- LStringReader: TStringReader;
- vJSON: string;
- begin
- //
- LStringReader := TStringReader.Create(Memo2.Text);
- //
- LJsonTextReader := TJsonTextReader.Create(LStringReader);
- LIterator := TJSONIterator.Create(LJsonTextReader);
- LIterator.Recurse; // prepare to enter array
- LIterator.Next;
- LIterator.Recurse; // enter array
- Memo1.Lines.Add(LIterator.Key); // Transaction
- LIterator.Recurse; // prepare to enter object
- LIterator.Next;
- LIterator.Recurse; // enter array object
- LIterator.Next;
- Memo1.Lines.Add(LIterator.Key +': '+ IntToStr(LIterator.AsInteger)); // id : 662713
- LIterator.Next;
- Memo1.Lines.Add(LIterator.Key +': '+ LIterator.AsString); // firstName : John
- LIterator.Next;
- Memo1.Lines.Add(LIterator.Key +': '+ LIterator.AsString); // lastName : Doe
- LIterator.Next;
- Memo1.Lines.Add(LIterator.Key +': '+ LIterator.AsDouble.ToString); // price : 2.1
- LIterator.Next;
- if LIterator.IsNull then
- Memo1.Lines.Add(LIterator.Key +': null'); // parent_id : null
- LIterator.Next;
- if LIterator.AsBoolean then
- Memo1.Lines.Add(LIterator.Key +': true') // validated : true
- else
- Memo1.Lines.Add(LIterator.Key +': false');
- LIterator.Return; // back to parent structure
- LIterator.Recurse; // prepare to enter object
- LIterator.Next;
- LIterator.Recurse; // enter object
- LIterator.Next;
- Memo1.Lines.Add(LIterator.Key +': '+ IntToStr(LIterator.AsInteger)); // id : 662714
- LIterator.Next('price'); // move to key 'price'
- Memo1.Lines.Add(LIterator.Key +': '+ LIterator.AsDouble.ToString); // price : 4.5
- LIterator.Next;
- if LIterator.IsNull then
- Memo1.Lines.Add(LIterator.Key +': null'); // parent_id : null
- LIterator.Return; // back to parent structure
- LIterator.Recurse; // prepare to enter object
- LIterator.Next;
- LIterator.Recurse; // enter object
- LIterator.Next;
- LIterator.Next('firstName'); // move to key 'firstName'
- Memo1.Lines.Add(LIterator.Key +': '+ LIterator.AsString); // firstName : Peter
- LIterator.Next;
- Memo1.Lines.Add(LIterator.Key +': '+ LIterator.AsString); // lastName : Jones
- end;
- procedure TfrmPrincipal.Button9Click(Sender: TObject);
- begin
- ShowMessage('Enviar email');
- end;
- function TfrmPrincipal.DataSetToJsonTxt(pDataSet: TDataSet): string;
- var
- ArrayJSon: TJSONArray;
- ObjJSon: TJSONObject;
- strJSon: TJSONString;
- intJSon: TJSONNumber;
- TrueJSon: TJSONTrue;
- FalseJSon: TJSONFalse;
- I: Integer;
- pField: TField;
- begin
- ArrayJSon := TJSONArray.Create;
- try
- pDataSet.First;
- while not pDataSet.Eof do
- begin
- ObjJSon := TJSONObject.Create;
- for pField in pDataSet.Fields do
- case pField.DataType of
- ftString:
- begin
- strJSon := TJSONString.Create(pField.AsString);
- ObjJSon.AddPair(pField.FieldName,strJSon);
- end;
- ftInteger:
- begin
- IntJSon := TJSONNumber.Create(pField.AsInteger);
- ObjJSon.AddPair(pField.FieldName,IntJSon);
- end;
- // ftBoolean:
- // case rdgEstadoDataset.ItemIndex of
- // 0:begin
- // TrueJSon := TJSONTrue.Create;
- // ObjJSon.AddPair(pField.FieldName,TrueJSon);
- // end;
- // 1:begin
- // FalseJSon := TJSONFalse.Create;
- // ObjJSon.AddPair(pField.FieldName,FalseJSon);
- // end;
- // end;
- else //casos gerais são tratados como string
- begin
- strJSon := TJSONString.Create(pField.AsString);
- ObjJSon.AddPair(pField.FieldName,strJSon);
- end;
- end;
- ArrayJSon.AddElement(ObjJSon);
- pDataSet.next;
- end;
- result := ArrayJSon.ToString;
- finally
- ArrayJSon.Free;
- end;
- end;
- procedure TfrmPrincipal.FormActivate(Sender: TObject);
- begin
- RESTClient.Accept := 'application/json, text/plain; q=0.9, text/html;q=0.8,';
- RESTClient.AcceptCharset := 'UTF-8, *;q=0.8';
- TabControl.ActiveTab := tabTeste;
- end;
- procedure TfrmPrincipal.FormClose(Sender: TObject; var Action: TCloseAction);
- begin
- FobjValueJson.Free;
- end;
- procedure TfrmPrincipal.SpeedButton1Click(Sender: TObject);
- begin
- Layout10.Visible := not Layout10.Visible;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement