jjw

写给自己的博客。 记录学习的点滴以备查。
随笔 - 127, 文章 - 0, 评论 - 8, 阅读 - 62728
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

delphi 自带的JSON序列 一

Posted on   jjw  阅读(317)  评论(0编辑  收藏  举报
复制代码
unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
  System.JSON.Serializers, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
type
  TRecA = record
    stringValue: string;
    datetimeValue: TDateTime;
    integerValue: Integer;
    doubleValue: Double;
  end;

  TRecB = record
    stringValue: string;
    datetimeValue: TDateTime;
    integerValue: Integer;
    doubleValue: Double;
  end;

  TRecAB = record
    A: TRecA;
    B: TRecB;
  end;
begin
  var AB: TRecAB;
  AB.A.stringValue := 'hello world';
  AB.A.datetimeValue := Now;
  AB.A.integerValue := 123;
  AB.A.doubleValue := 3.1415926;

  AB.B.stringValue := 'HELLO WORLD';
  AB.B.datetimeValue := Now;
  AB.B.integerValue := 666;
  AB.B.doubleValue := 3.1415927;

  var Arr: TArray<TRecAB>;

  SetLength(Arr, 1);

  Arr[0] := AB;

  var s := TJsonSerializer.Create;
  Memo1.Text := s.Serialize<TArray<TRecAB>>(Arr);

  var d := TJsonSerializer.Create;
  var EmptyArray: TArray<Integer> := d.Deserialize<TArray<Integer>>('[]');
  Caption := Length(EmptyArray).ToString;
end;

end.
复制代码

记不清什么时候有的,个人感觉还是很好用的. 可以控制 序列化属性,字段(私有)等。

相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示