標籤:delphi json sql list
Json是一種輕量級資料轉送格式,廣泛應用互連網和各應用中,json主要採用鍵值對來表示資料項目,多個資料項目之間用逗號分隔,也可以用於數組。下面注重介紹一下在delphi中使用json,在delphi中使用json常用superobject單元檔案,該檔案可以在網上下載,最初接觸json是在2011年,好久沒用這不剛好有項目要用到又折騰了好久,下面做了一個簡單的Demo,方便以後忘了能隨時查看,具體的json使用可以參看萬一老師的部落格,記錄的很詳細,下面的demo主要是將資料庫記錄轉換為json格式,然後進行解析。具體源碼如下。
建立一個delphi應用程式,在表單上放置table組件,串連資料DBDEMOS,串連表customer.db,active設定為true。具體檔案如下:
delphi工程檔案如下
program Project1;
uses
Forms,
Unit1 in ‘Unit1.pas‘ {Form1};
{$R *.res}
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
單元檔案如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids, DBGrids, DB, DBTables;
type
TForm1 = class(TForm)
mmo1: TMemo;
tbl1: TTable;
ds1: TDataSource;
btn1: TButton;
btn2: TButton;
btn3: TButton;
procedure btn1Click(Sender: TObject);
procedure btn2Click(Sender: TObject);
procedure btn3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses superobject;
procedure TForm1.btn1Click(Sender: TObject);
var
jo,jt:ISuperObject;
begin
jo:=SO();
jt:=SO();
jo.S[‘xm‘]:=‘張三‘;
jo.I[‘age‘]:=25;
jo.S[‘sex‘]:=‘男‘;
jt.O[‘person‘]:=jo;
ShowMessage(jt.AsString);
ShowMessage(jt.O[‘person‘].S[‘xm‘]);
end;
procedure TForm1.btn2Click(Sender: TObject);
var
i:Integer;
jfields,jitems,jo:ISuperObject;
begin
mmo1.Clear;
jitems:=SA([]);
jo:=SO();
with tbl1 do
begin
First;
while not eof do
begin
jfields:=SO();
for i:=0 to FieldCount-1 do
begin
if Fields[i].DataType=ftDateTime then
jfields.S[Fields[i].FieldName]:=FormatDateTime(‘yyyy-mm-dd hh:mm:ss‘,Fields[i].AsDateTime)
else
jfields.S[Fields[i].FieldName]:=Fields[i].AsString;
end;
jitems.AsArray.Add(jfields);
Next;
end;
end;
jo.O[‘records‘]:=jitems;
mmo1.Lines.Add(jo.AsString);
end;
procedure TForm1.btn3Click(Sender: TObject);
var
jo,m:ISuperobject;
i,j:Integer;
tt:TSuperArray;
s:string;
lst:TStringList;
begin
if mmo1.Text<>‘‘ then
begin
lst:=TStringList.Create;
jo:=so(mmo1.Text);
tt:=jo.O[‘records‘].AsArray;
for i:=0 to tt.Length-1 do
begin
s:=‘‘;
for j:=0 to tbl1.FieldCount-1 do
begin
if s=‘‘ then
s:=tt.O[i].S[tbl1.Fields[j].FieldName]
else
s:=s+‘ ‘+tt.O[i].S[tbl1.Fields[j].FieldName]
end;
lst.Add(s);
end;
ShowMessage(lst.Text) ;
lst.Free;
end;
end;
end.
dfm檔案如下:
object Form1: TForm1
Left = 312
Top = 70
Width = 410
Height = 382
Caption = ‘json讀寫‘
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = ‘MS Sans Serif‘
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object mmo1: TMemo
Left = 0
Top = 0
Width = 394
Height = 249
Align = alTop
TabOrder = 0
end
object btn1: TButton
Left = 23
Top = 264
Width = 75
Height = 25
Caption = ‘簡單讀寫‘
TabOrder = 1
OnClick = btn1Click
end
object btn2: TButton
Left = 121
Top = 264
Width = 105
Height = 25
Caption = ‘讀資料庫記錄‘
TabOrder = 2
OnClick = btn2Click
end
object btn3: TButton
Left = 253
Top = 263
Width = 75
Height = 25
Caption = ‘解析json‘
TabOrder = 3
OnClick = btn3Click
end
object tbl1: TTable
Active = True
DatabaseName = ‘DBDEMOS‘
TableName = ‘customer.db‘
Left = 288
Top = 152
end
object ds1: TDataSource
DataSet = tbl1
Left = 104
Top = 176
end
end
其中有些版本superobject的hash方法有編譯指令,我使用時報錯,將其去掉改為如下:
class function TSuperAvlEntry.Hash(const k: SOString): Cardinal;
var
h: cardinal;
i: Integer;
begin
h := 0;
for i := 1 to Length(k) do
h := h*129 + ord(k[i]) + $9e370001;
Result := h;
end;
程式運行結果如下:
點擊簡單讀寫
讀資料庫記錄
解析json
superobject單元還提供了許多方法,在此不一一列舉使用,以後使用中再慢慢總結。