fpjson

fpjson

fpc官方帮助:https://wiki.freepascal.org/fcl-json

fcl-json 是一个JSON实现。

它包含以下单位:

  • fpjson:实现 TJsonData 及其子项的基本单元,例如 TJsonObject
  • jsonParser:实现 TJsonParser,在下面From JsonViewer示例中使用
  • jsonConf:实现了 TJsonConfig,它可以方便地从/向文件读取/写入应用程序数据
  • jsonScanner:json 源码词法分析器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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;

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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;

  

posted @   delphi中间件  阅读(401)  评论(1编辑  收藏  举报
编辑推荐:
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 我与微信审核的“相爱相杀”看个人小程序副业
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· spring官宣接入deepseek,真的太香了~
历史上的今天:
2017-08-12 DELPHI7下SUPPEROBJECT遍历子对象的name和value
2017-08-12 superobject使用方法
点击右上角即可分享
微信分享提示