首页 > 代码库 > delphi json uLkJSON
delphi json uLkJSON
delphi 7 json 做个笔记,留着以后用
--源码
unit Umain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
mmo1: TMemo;
mmo2: TMemo;
Button3: TButton;
Button4: TButton;
Button5: TButton;
Button6: TButton;
Button7: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure Button7Click(Sender: TObject);
private
{ Private declarations }
str: string;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses uLkJSON;
procedure TForm1.Button1Click(Sender: TObject);
var
js: TlkJSONobject;
temp: string;
begin
// js := TlkJSONobject.Create();
// js.Add(‘name‘, ‘张三‘);
// js.Add(‘sex‘, ‘男‘);
// js.Add(‘age‘, ‘20‘);
str := ‘{"name":"张三","sex":"男","age":"20","data":[{"abc":"16.346","def":"3223.32"}]}‘;
// str := TlkJSON.GenerateText(js);
mmo1.Text := str;
//FreeAndNil(js);
end;
procedure TForm1.Button2Click(Sender: TObject);
var
js: TlkJSONobject;
i: Integer;
begin
js := TlkJSON.ParseText(mmo1.Text) as TlkJSONobject;
//mmo2.Lines.Add(js.getString(‘name‘));
//mmo2.Lines.Add(js.getString(‘sex‘));
// mmo2.Lines.Add(js.getString(‘age‘));
for i := 0 to js.Count - 1 do
begin
mmo2.Lines.Add(js.getString(i));
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
var
js: TlkJSONobject;
i, j, k: Integer;
jsRow: TlkJSONlist;
jsCol: TlkJSONobject;
jsField: TlkJSONobject;
begin
js := TlkJSON.ParseText(mmo1.Text) as TlkJSONobject;
for i := 0 to js.Count - 1 do
begin
if js.FieldByIndex[i] is TlkJSONlist then
begin
jsRow := js.FieldByIndex[i] as TlkJSONlist;
for j := 0 to jsRow.Count-1 do
begin
jsCol:= jsRow.Child[j] as TlkJSONobject;
for k := 0 to jsCol.Count-1 do
begin
mmo2.Lines.Add( jsCol.NameOf[k]+‘+++‘+jsCol.getString(k));
end;
end;
end
else
begin
mmo2.Lines.Add( js.NameOf[i]+‘===‘+ js.getString(i));
end;
end;
end;
procedure TForm1.Button4Click(Sender: TObject);
var
js,jscol:TlkJSONobject;
i,j,k:Integer;
jsrow:TlkJSONlist;
begin
Button1.Click;
js:= TlkJSON.ParseText(mmo1.Text) as TlkJSONobject;
for i := 0 to js.Count-1 do
begin
if js.FieldByIndex[i] is TlkJSONlist then
begin
jsrow:=js.FieldByIndex[i] as TlkJSONlist ;
for j :=0 to jsrow.Count-1 do
begin
jscol:= jsrow.Child[j] as TlkJSONobject;
for k := 0 to jscol.Count-1 do
begin
mmo2.Lines.Add(jscol.NameOf[K] +‘:==:‘ + jscol.getString(k));
end;
end;
end
else
begin
mmo2.Lines.Add(js.NameOf[i] +‘:‘ + js.getString(i));
end;
end;
end;
procedure TForm1.Button5Click(Sender: TObject);
var
js,jstemp: TlkJSONobject;
jslist:TlkJSONlist;
temp: string;
begin
js := TlkJSONobject.Create();
jstemp:=TlkJSONobject.Create();
jstemp.Add(‘desf‘,‘16.346‘);
jstemp.Add(‘abc‘,‘13223.32‘);
jslist:=TlkJSONlist.Create;
jslist.Add(jstemp);
js.Add(‘name‘, ‘张三‘);
js.Add(‘sex‘, ‘男‘);
js.Add(‘age‘, ‘20‘);
js.Add(‘data‘,jslist);
// ATickerList := ‘[{‘ + ATickerList + ‘}]‘;
// ATickerList := ‘[{‘ + ATickerList + ‘}]‘;
//js.Add(‘data‘,‘[{‘ + ‘}]‘ );
//str := ‘{"name":"张三","sex":"男","age":"20","data":[{"acbd":"16.346","def":"3223.32"}]}‘;
str := TlkJSON.GenerateText(js);
mmo1.Text := str;
FreeAndNil(js);
end;
procedure TForm1.Button6Click(Sender: TObject);
var
jsM:TlkJSONstreamed;
js:TlkJSONobject;
begin
js:=TlkJSONobject.Create;
jsM:=TlkJSONstreamed.Create;
js.Add(‘name‘,‘张三‘);
js.Add(‘age‘,‘20‘);
js.Add(‘sex‘,‘男‘);
jsM.SaveToFile(js,‘c:\1.txt‘);
end;
procedure TForm1.Button7Click(Sender: TObject);
var
jsM:TlkJSONstreamed;
js:TlkJSONobject;
i:Integer;
begin
jsM:=TlkJSONstreamed.Create;
js:= jsm.LoadFromFile(‘c:\1.txt‘) as TlkJSONobject;
for i := 0 to js.Count-1 do
begin
mmo2.Lines.Add( js.NameOf[i]+‘==‘+ js.getString(i) );
end;
end;
end.
--窗体文件
object Form1: TForm1
Left = 213
Top = 226
Width = 620
Height = 563
Caption = ‘Form1‘
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 Button1: TButton
Left = 256
Top = 32
Width = 75
Height = 25
Caption = ‘加入json‘
TabOrder = 0
OnClick = Button1Click
end
object Button2: TButton
Left = 256
Top = 80
Width = 75
Height = 25
Caption = ‘解析json‘
TabOrder = 1
OnClick = Button2Click
end
object mmo1: TMemo
Left = 8
Top = 24
Width = 233
Height = 337
ScrollBars = ssBoth
TabOrder = 2
end
object mmo2: TMemo
Left = 344
Top = 24
Width = 233
Height = 337
ScrollBars = ssBoth
TabOrder = 3
end
object Button3: TButton
Left = 256
Top = 120
Width = 75
Height = 25
Caption = ‘解析2‘
TabOrder = 4
OnClick = Button3Click
end
object Button4: TButton
Left = 256
Top = 168
Width = 75
Height = 25
Caption = ‘解析3‘
TabOrder = 5
OnClick = Button4Click
end
object Button5: TButton
Left = 256
Top = 216
Width = 75
Height = 25
Caption = ‘Button5‘
TabOrder = 6
OnClick = Button5Click
end
object Button6: TButton
Left = 248
Top = 272
Width = 75
Height = 25
Caption = ‘保存到文件‘
TabOrder = 7
OnClick = Button6Click
end
object Button7: TButton
Left = 256
Top = 312
Width = 75
Height = 25
Caption = ‘Button7‘
TabOrder = 8
OnClick = Button7Click
end
end
delphi json uLkJSON