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 supports XML, JSON, and binary.
And now, I would like to introduce you to the DataSetConverter4Delphi 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
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; |
Be sure to check out the DataSetConverter4Delphi library!
Next, learn how you can append to String Python and answer other questions about String Operations in C++ software in this article.
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition
Converting DataSet to JSON is easy !
How can convert JSON to DataSet ( Create fiels automatically and fill them from an JSON string)?
for example how to create ClientDataSet fiels automatically from this simple JSON string and fill it :
jsonstring=’
[
{
“id”: 1,
“name”: “Mateus Vicente”,
“country”: “Brazil”
},
{
“id”: 2,
“name”: “Vinicius Sanchez”,
“country”: “Brazil”
},
{
“id”: 3,
“name”: “Julio Senha”,
“country”: “Brazil”
},
{
“id”: 4,
“name”: “André Dias”,
“country”: “Brazil”
},
{
“id”: 5,
“name”: “Luis Gustavo”,
“country”: “Brazil”
},
{
“id”: 6,
“name”: “José Junior”,
“country”: “Brazil”
},
{
“id”: 7,
“name”: “Fagner Granella”,
“country”: “Brazil”
},
{
“id”: 8,
“name”: “Eduardo Viana”,
“country”: “Brazil”
},
{
“id”: 9,
“name”: “Alan Echer”,
“country”: “Brazil”
}
]
‘;
Procedure CreateFieldsAndFillDataSetByJSonString(cds:TClientDataset;JString:String)
….