Advertisement
filhotecmail

Convertendo Json to Dataset

Aug 16th, 2017
573
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.22 KB | None | 0 0
  1. uses
  2.   REST.Response.Adapter, System.JSON;
  3.  
  4. procedure JsonToDataset(aDataset : TDataSet; aJSON : string);
  5. var
  6.   JObj: TJSONArray;
  7.   vConv : TCustomJSONDataSetAdapter;
  8. begin
  9.   if (aJSON = EmptyStr) then
  10.   begin
  11.     Exit;
  12.   end;
  13.  
  14.   JObj := TJSONObject.ParseJSONValue(aJSON) as TJSONArray;
  15.   vConv := TCustomJSONDataSetAdapter.Create(Nil);
  16.  
  17.   try
  18.     vConv.Dataset := aDataset;
  19.     vConv.UpdateDataSet(JObj);
  20.   finally
  21.     vConv.Free;
  22.     JObj.Free;
  23.   end;
  24. end;
  25.  
  26. //**************************************************************************************************************
  27.  
  28. unit Unit1;
  29.  
  30. interface
  31.  
  32. uses
  33.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  34.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, FireDAC.Stan.Intf, FireDAC.Stan.Option,
  35.   FireDAC.Stan.Param, FireDAC.Stan.Error, FireDAC.DatS, FireDAC.Phys.Intf,
  36.   FireDAC.DApt.Intf, Vcl.StdCtrls, Data.DB, FireDAC.Comp.DataSet,
  37.   FireDAC.Comp.Client, Vcl.Grids, Vcl.DBGrids, Datasnap.DBClient;
  38.  
  39. type
  40.   TForm1 = class(TForm)
  41.     DBGrid1: TDBGrid;
  42.     DataSource1: TDataSource;
  43.     DBGrid2: TDBGrid;
  44.     DataSource2: TDataSource;
  45.     FDMemTable1: TFDMemTable;
  46.     Button1: TButton;
  47.     ClientDataSet1: TClientDataSet;
  48.     procedure Button1Click(Sender: TObject);
  49.   private
  50.     { Private declarations }
  51.   public
  52.     { Public declarations }
  53.   end;
  54.  
  55. var
  56.   Form1: TForm1;
  57.  
  58. implementation
  59.  
  60. {$R *.dfm}
  61.  
  62. uses
  63.   REST.Response.Adapter, System.JSON;
  64.  
  65. procedure JsonToDataset(aDataset : TDataSet; aJSON : string);
  66. var
  67.   JObj: TJSONArray;
  68.   vConv : TCustomJSONDataSetAdapter;
  69. begin
  70.   if (aJSON = EmptyStr) then
  71.   begin
  72.     Exit;
  73.   end;
  74.  
  75.   JObj := TJSONObject.ParseJSONValue(aJSON) as TJSONArray;
  76.   vConv := TCustomJSONDataSetAdapter.Create(Nil);
  77.  
  78.   try
  79.     vConv.Dataset := aDataset;
  80.     vConv.UpdateDataSet(JObj);
  81.   finally
  82.     vConv.Free;
  83.     JObj.Free;
  84.   end;
  85. end;
  86.  
  87. procedure TForm1.Button1Click(Sender: TObject);
  88. const
  89.   MEU_JSON =
  90.   '[{'+
  91.     '"Titulo": "Ate que a sorte nos separe",'+
  92.     '"duracao": "120 min"'+
  93.   '}, {'+
  94.     '"Titulo": "Matrix",'+
  95.     '"duracao": "140 min"'+
  96.   '}]';
  97. begin
  98.   JsonToDataset(FDMemTable1, MEU_JSON);
  99.   JsonToDataset(ClientDataSet1, MEU_JSON);
  100. end;
  101.  
  102. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement