首页 > 其他分享 >mormot2 json例子

mormot2 json例子

时间:2023-02-08 13:11:13浏览次数:42  
标签:end name TObject json 例子 mormot2 procedure Sender

mormot2 json例子

 

unit Unit1;
/// <author>cxg 2023-2-8</author>
/// mormot2 json demo

interface

uses
  mormot.core.text, mormot.core.json, Winapi.Windows, Winapi.Messages,
  System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls,
  Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TTest = record            //结构体
    id: Integer;
    name: string;
  end;

  TObj = class                //对象
  private
    fid: Integer;
    fname: string;
  published
    property id: Integer read fid write fid;
    property name: string read fname write fname;
  end;

  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
//record to json
begin
  var t: TTest;
  t.id := 1;
  t.name := '名字';
  Memo1.Text := mormot.core.json.RecordSaveJson(t, TypeInfo(TTest));  //{"id":1,"name":"name"}
end;

procedure TForm1.Button2Click(Sender: TObject);
//json to record
begin
  var t: TTest;
  mormot.core.json.RecordLoadJson(t, Memo1.Text, TypeInfo(TTest));
  ShowMessage(t.name);   //名字
end;

procedure TForm1.Button3Click(Sender: TObject);
//object to json
begin
  var o: TObj := TObj.Create;
  o.id := 2;
  o.name := '名字';
  Memo1.Text := mormot.core.text.ObjectToJson(o);  //{"id":2,"name":"名字"}
  o.Free;
end;

procedure TForm1.Button4Click(Sender: TObject);
//json to object
begin
  var o: TObj := TObj.Create;
  mormot.core.json.ObjectLoadJson(o, Memo1.Text);
  ShowMessage(o.name);    //名字
  o.Free;
end;

end.

 

标签:end,name,TObject,json,例子,mormot2,procedure,Sender
From: https://www.cnblogs.com/hnxxcxg/p/17101359.html

相关文章