登录 用户中心() [退出] 后台管理 注册
   
您的位置: 首页 >> SoftHub关联区 >> 主题: 使用 Lazarus 读写 JSON [zt]     [回主站]     [分站链接]
使用 Lazarus 读写 JSON [zt]
clq
浏览(285) - 2022-10-23 18:34:46 发表 编辑

关键字:


https://www.jianshu.com/p/8728ffc955da

使用 Lazarus 读写 JSON
wyrover
2016.05.05 14:51:04
字数 19阅读 2,102

TJSONObject TJSONData TJSONArray

uses fpjson, jsonparser;

procedure JSONTest;
var
jData : TJSONData;
jObject : TJSONObject;
jArray : TJSONArray;
s : string;
begin

// this is only a minimal sampling of what can be done with this API

// create from string
jData := GetJSON('{"Fld1" : "Hello", "Fld2" : 42, "Colors" : ["Red", "Green", "Blue"]}');

// output as a flat string
s := jData.AsJSON;

// output as nicely formatted JSON
s := jData.FormatJSON;

// cast as TJSONObject to make access easier
jObject := TJSONObject(jData);

// retrieve value of Fld1
s := jObject.Get('Fld1');

// change value of Fld2
jObject.Integers['Fld2'] := 123;

// retrieve the second color
s := jData.FindPath('Colors[1]').AsString;

// add a new element
jObject.Add('Happy', True);

// add a new sub-array
jArray := TJSONArray.Create;
jArray.Add('North');
jArray.Add('South');
jArray.Add('East');
jArray.Add('West');
jObject.Add('Directions', jArray);

end;

TJSONConfig

uses jsonConf;

procedure TfmMain.SaveOptionsPos;
var
c: TJSONConfig;
begin
c:= TJSONConfig.Create(nil);
try
c.Filename:= GetAppPath(cFileHistory);
c.SetValue('/dialog/max', WindowState=wsMaximized);
if WindowState<>wsMaximized then
begin
c.SetValue('/dialog/posx', Left);
c.SetValue('/dialog/posy', Top);
c.SetValue('/dialog/sizex', Width);
c.SetValue('/dialog/sizey', Height);
end;
finally
c.Free;
end;
end;

procedure TfmMain.LoadOptionsPos;
var
nLeft, nTop, nW, nH: integer;
c: TJSONConfig;
begin
c:= TJSONConfig.Create(nil);
try
c.Filename:= GetAppPath(cFileHistory);

nLeft:= c.GetValue('/dialog/posx', Left);
nTop:= c.GetValue('/dialog/posy', Top);
nW:= c.GetValue('/dialog/sizex', Width);
nH:= c.GetValue('/dialog/sizey', Height);
SetBounds(nLeft, nTop, nW, nH);

if c.GetValue('/dialog/max', false) then
WindowState:= wsMaximized;
finally
c.Free;
end;
end;

TJSONParser

procedure TMainForm.OpenFile(Const AFileName : String);

Var
S : TFileStream;
P : TJSONParser;
D : TJSONData;
begin
S:=TFileStream.Create(AFileName,fmOpenRead);
try
P:=TJSONParser.Create(S);
try
P.Strict:=FStrict;
D:=P.Parse;
finally
P.Free;
end;
finally
S.Free;
end;
FFileName:=AFileName;
SetCaption;
FreeAndNil(FRoot);
FRoot:=D;
ShowJSONDocument;
end;

procedure TMainForm.ShowJSONDocument;

begin
With TVJSON.Items do
begin
BeginUpdate;
try
TVJSON.Items.Clear;
SHowJSONData(Nil,FRoot);
With TVJSON do
If (Items.Count>0) and Assigned(Items[0]) then
begin
Items[0].Expand(False);
Selected:=Items[0];
end;
finally
EndUpdate;
end;
end;
end;

procedure TMainForm.ShowJSONData(AParent : TTreeNode; Data : TJSONData);

Var
N,N2 : TTreeNode;
I : Integer;
D : TJSONData;
C : String;
S : TStringList;

begin
N:=Nil;
if Assigned(Data) then
begin
Case Data.JSONType of
jtArray,
jtObject:
begin
If (Data.JSONType=jtArray) then
C:=SArray
else
C:=SObject;
N:=TVJSON.Items.AddChild(AParent,Format(C,[Data.Count]));
S:=TstringList.Create;
try
For I:=0 to Data.Count-1 do
If Data.JSONtype=jtArray then
S.AddObject(IntToStr(I),Data.items[i])
else
S.AddObject(TJSONObject(Data).Names[i],Data.items[i]);
If FSortObjectMembers and (Data.JSONType=jtObject) then
S.Sort;
For I:=0 to S.Count-1 do
begin
N2:=TVJSON.Items.AddChild(N,S[i]);
D:=TJSONData(S.Objects[i]);
N2.ImageIndex:=ImageTypeMap[D.JSONType];
N2.SelectedIndex:=ImageTypeMap[D.JSONType];
ShowJSONData(N2,D);
end
finally
S.Free;
end;
end;
jtNull:
N:=TVJSON.Items.AddChild(AParent,SNull);
else
N:=TVJSON.Items.AddChild(AParent,Data.AsString);
end;
If Assigned(N) then
begin
N.ImageIndex:=ImageTypeMap[Data.JSONType];
N.SelectedIndex:=ImageTypeMap[Data.JSONType];
N.Data:=Data;
end;
end;
end;

links

http://lazplanet.blogspot.jp/2014/09/a-simple-json-parsing-example.html


总数:0 页次:1/0 首页 尾页  
总数:0 页次:1/0 首页 尾页  


所在合集/目录



发表评论:
文本/html模式切换 插入图片 文本/html模式切换


附件:



NEWBT官方QQ群1: 276678893
可求档连环画,漫画;询问文本处理大师等软件使用技巧;求档softhub软件下载及使用技巧.
但不可"开车",严禁国家敏感话题,不可求档涉及版权的文档软件.
验证问题说明申请入群原因即可.

Copyright © 2005-2020 clq, All Rights Reserved
版权所有
桂ICP备15002303号-1