Delphi Robust DataSet To JSON And JSON To DataSet API For Delphi

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,199
Credits
4,009
Robust DataSet To JSON And JSON To DataSet API For Delphi
February 12, 2021 By Muminjon

Most of the time, developers tend to use DataSet to manipulate the data in their applications. DataSet is helpful because it stores data in the dataset’s fields array property. You can easily access these values by number or by name. Moreover, each field can be used to read or modify the current record’s data by using its Value property or type-specific properties such as AsString, AsInteger, and so on. FireDAC itself has a built in SaveToFile and LoadFromFile (plus SaveToStream and LoadFromStream) for dataSets which Для просмотра ссылки Войди или Зарегистрируйся.

And now, I would like to introduce you to the Для просмотра ссылки Войди или Зарегистрируйся API. This is an API to convert JSON objects for DataSet and also doing the reverse process, like converting DataSet into JSON. And it works with the TDataSet, and TJSONObject, TJSONArray classes.

Furthermore, it is easy to add to your project, you just need to specify the path of the modules and you are good to go.

Convert DataSet to JSON​

Код:
uses
  DataSetConverter4D,
  DataSetConverter4D.Impl;   
 
var
  ja: TJSONArray;
  jo: TJSONObject;
begin
  fCdsCustomers.DataSetField := nil;
  fCdsCustomers.CreateDataSet;
 
  fCdsCustomers.Append;
  fCdsCustomers.FieldByName('Id').AsInteger := 1;
  fCdsCustomers.FieldByName('Name').AsString := 'Customers 1';
  fCdsCustomers.FieldByName('Birth').AsDateTime := StrToDateTime('22/01/2014 14:05:03');
  fCdsCustomers.Post;
 
  fCdsCustomers.Append;
  fCdsCustomers.FieldByName('Id').AsInteger := 2;
  fCdsCustomers.FieldByName('Name').AsString := 'Customers 2';
  fCdsCustomers.FieldByName('Birth').AsDateTime := StrToDateTime('22/01/2014 14:05:03');
  fCdsCustomers.Post;
 
  //Convert all records   
  ja := TConverter.New.DataSet(fCdsCustomers).AsJSONArray;
 
  //Convert current record
  jo := TConverter.New.DataSet.Source(fCdsCustomers).AsJSONObject;
 
  ja.Free;
  jo.Free;
end;
Для просмотра ссылки Войди или Зарегистрируйся