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

时间: 2024-10-12 04:35:38

delphi json uLkJSON的相关文章

delphi Json操作

Creating a Simple JSON Object The following code segment demonstrates the creation of a simple JSON object containing one JSON pair. Once this object is created, its ToString method is invoked, assigning the returned string representation of the JSON

delphi json用法

最近接触到json,开始还有点抵触,用多了感觉蛮好玩的,特别是里面什么都可以放,整形I,数组A,对象O,布尔型B等等,而且delphi用起来也很方便,可以直接放文本,当变量,里面的值也可以随时改变,真的太方便了.取值时,也是一层一层的取,若前面都一样可以用with...do...去掉不要的层级.需要注意的是大小写取值时要区分.用法:uses Superobject, Sperjsondelphi里有json单元. procedure TForm2.SuperObjectClick(Sender:

delphi json(CDS包含了Delta数据包)

在delphi中,数据集是最常用数据存取方式.因此,必须建立JSON与TDataSet之间的互转关系,实现数据之间通讯与转换.值得注意的是,这只是普通的TDataset与JSON之间转换,由于CDS包含了Delta数据包,其数据格式远比普通的TDataset更复杂. 数据集字段信息,是一个完整的字典信息.因此,我们在JSON必须也建立字典信息,才能创建数据集的字段信息.我们设置其JSON信息如下: COLS:[字段列表信息],如: "Cols":[{"JsonType&quo

Delphi Json

superobject.pas单元对json的解析非常方便, 力荐 下面演示对如下json的解析 { "userid": "u0001", "info": [ { "url": "rtmp://192.168.3.159/livepkgr/66?adbe-live-event=liveevent", "lng": 105.97505, "lat": 34.271555

如何解析DELPHI XE5服务器返回的JSON数据(翻译)及中文乱码

[plain] view plain copy print? <span style="font-size:14px;">一直想找如何解析JSON数据的说,今天终于找到有人发帖子了.之前有人说用superobject,Tlkjson,delphi json library,delphi  web unit等等.其实我是想找比较简单的解析方式.解析简单的json.下面是转载的坦然的源码. </span> [delphi] view plain copy print

xe5 delphi 解析 json

xe5 delphi 解析 json 2014-03-04 17:35:41|  分类: mobile |  标签:delphi  json  mobile  |举报 |字号大中小 订阅 最简单的JSON大致像这样 { "date":"周二(今天, 实时:12℃)", "dayPictureUrl":"http://api.map.baidu.com/images/weather/day/duoyun.png", "

YxdJSON - Delphi 高性能 JSON 库(支持RTTI和序列化操作)

Delphi 高性能 JSON 库(支持RTTI和序列化操作) 支持平台: Windows, Android, iOS, Mac OS https://github.com/yangyxd/YxdJson

转:Delphi语言最好的JSON代码库 mORMot学习笔记1

mORMot没有控件安装,直接添加到lib路径,工程中直接添加syncommons,syndb等到uses里 --------------------------------------------------------- 在进行网络编程中需要JSON对象的构建与解析,这个Delphi XE+自带:{$IF CompilerVersion>22}, System.JSon{$ELSE}, DBXJSON{$IFEND}不过,不好用,大家更喜欢SuperObject.我本人以前一直用JsonDa

Delphi中JSon SuperObject 使用:数据集与JSON对象互转

在delphi中,数据集是最常用数据存取方式.因此,必须建立JSON与TDataSet之间的互转关系,实现数据之间通讯与转换.值得注意的是,这只是普通的TDataset与JSON之间转换,由于CDS包含了Delta数据包,其数据格式远比普通的TDataset更复杂. 数据集字段信息,是一个完整的字典信息.因此,我们在JSON必须也建立字典信息,才能创建数据集的字段信息.我们设置其JSON信息如下: COLS:[字段列表信息],如: "Cols":[{"JsonType&quo