Sign in Join
Wiki copytemporary

fpjsondataset: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: fpjsondataset

OldNewLine
1+**fpjsondataset** is an fcl-db unit containing a dataset class for accessing JSON data as simple table structures.
2+
3+## TJSONDataSet
4+
5+TJSONDataSet is a dataset class for using JSON arrays for data handling.
6+
7+> **Note:** TJSONDataSet is included in Free Pascal but not visible in Lazarus' component palette.
8+
9+### Example
10+
11+1. Create a new application project
12+2. Place a DB Grid on the form (DBGrid1)
13+3. Place a DataSource on the form (DataSource1)
14+4. Connect DBGrid1.Datasource with DataSource1
15+5. Create a JSON dataset via code
16+
17+``` pascal
18+unit Unit1;
19+
20+{$mode objfpc}{$H+}
21+
22+interface
23+
24+uses
25+ Classes, SysUtils, Forms, Controls, Graphics, Dialogs, DBCtrls, DBGrids, DB,
26+ fpjson, fpjsondataset;
27+
28+type
29+
30+ TForm1 = class(TForm)
31+ DataSource1: TDataSource;
32+ DBGrid1: TDBGrid;
33+ procedure FormCreate(Sender: TObject);
34+ private
35+ JSONDataSet: TJSONDataSet;
36+ end;
37+
38+var
39+ Form1: TForm1;
40+
41+const
42+ // this is the sample JSON data used in the demo
43+ JSON_STRING = '['
44+ +'{"Author ID":"409-56-7008","Last Name":"Bennet","First Name":"Abraham","Active": False},'
45+ +'{"Author ID":"213-46-8915","Last Name":"Green","First Name":"Marjorie","Active": True}'
46+ +']';
47+
48+implementation
49+
50+{$R *.lfm}
51+
52+procedure TForm1.FormCreate(Sender: TObject);
53+var
54+ data: TJSONArray;
55+begin
56+ // parse the JSON string
57+ data := GetJSON(JSON_STRING) as TJSONArray;
58+ // create a new TJSONDataSet
59+ JSONDataSet := TJSONDataSet.Create(Self);
60+ // you'll need to create the FieldDefs manually
61+ // ftString requires a length specified
62+ JSONDataSet.FieldDefs.Add('Author ID', ftString, 11, True);
63+ JSONDataSet.FieldDefs.Add('Last Name', ftString, 40);
64+ JSONDataSet.FieldDefs.Add('First Name', ftString, 20);
65+ JSONDataSet.FieldDefs.Add('Active', ftBoolean);
66+ // set OwnsData to True, and the JSON data will be destroyed when the dataset is destroyed
67+ JSONDataSet.OwnsData := True;
68+ // the JSON data is an array of objects
69+ JSONDataSet.RowType := rtJSONObject;
70+ // assign the JSON data to the dataset
71+ JSONDataSet.Rows := data;
72+ // activate the dataset
73+ JSONDataSet.Active := True;
74+ // assign the dataset to the datasource
75+ DataSource1.DataSet := JSONDataSet;
76+end;
77+
78+end.
79+
80+```
81+
82+---
83+*From the Free Pascal wiki, page "fpjsondataset", as it stood on 2021-04-01 after a change by Socke, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
84+Category: Databases
85+Category: FCL

Back to history