quicklib json序列
quicklib面向MODEL的JSON序列。
unit Unit2; /// <author>cxg 2022-6-14</author> interface uses quick.Json.Serializer, Quick.MemoryCache.Serializer.Json, System.Generics.Collections, Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls; type TPerson = class private FName: string; FAge: integer; published property Name: string read FName write FName; property Age: Integer read FAge write FAge; end; TPersonList = TObjectList<TPerson>; // TPersonArr = TArray<TPerson>; TPersons = class private FPersons: TPersonList; public constructor Create; destructor Destroy; override; published property Persons: TPersonList read FPersons write FPersons; end; type TForm2 = class(TForm) Button1: TButton; Memo1: TMemo; Button2: TButton; Button3: TButton; Button4: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure FormCreate(Sender: TObject); procedure Button3Click(Sender: TObject); procedure Button4Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form2: TForm2; implementation {$R *.dfm} procedure TForm2.Button1Click(Sender: TObject); //obj2json begin var a: TPerson := TPerson.Create; a.Name := '张三'; a.Age := 8; var b: TPerson := TPerson.Create; b.Name := '李四'; b.Age := 88; var p: TPersons := TPersons.Create; p.Persons.Add(a); p.Persons.Add(b); var serial: TJsonSerializer := TJsonSerializer.Create(slPublishedProperty); Memo1.Lines.Add(serial.ObjectToJson(p)); //{"Persons":[{"Name":"\u5F20\u4E09","Age":8},{"Name":"\u674E\u56DB","Age":88}]} p.Free; serial.Free; end; { TPersons } constructor TPersons.Create; begin FPersons := TPersonList.Create(True); end; destructor TPersons.Destroy; begin FPersons.Free; inherited; end; procedure TForm2.Button2Click(Sender: TObject); //json2obj begin var p: TPersons := TPersons.Create; var serial: TJsonSerializer := TJsonSerializer.Create(slPublishedProperty); serial.JsonToObject(p, Memo1.Text); for var a: TPerson in p.Persons do Memo1.Lines.Add(a.Name); //张三 李四 serial.Free; p.Free; end; procedure TForm2.Button3Click(Sender: TObject); begin var a: TPerson := TPerson.Create; a.Name := '张三'; a.Age := 8; var b: TPerson := TPerson.Create; b.Name := '李四'; b.Age := 88; var p: TArray<TObject>; SetLength(p, 2); p[0] := a; p[1] := b; var s: TCacheJsonSerializer := TCacheJsonSerializer.Create; Memo1.Lines.Add(s.Serialize(p)); s.Free; end; procedure TForm2.Button4Click(Sender: TObject); begin var s: TCacheJsonSerializer := TCacheJsonSerializer.Create; var p: TArray<TObject>; s.Deserialize(Memo1.Text, p); for var i := 0 to 1 do Memo1.Lines.Add(TPerson(p[i]).Name); s.Free; end; procedure TForm2.FormCreate(Sender: TObject); begin Memo1.Clear; end; end.
标签:quicklib,TPerson,Create,TObject,var,json,序列,end,procedure From: https://www.cnblogs.com/hnxxcxg/p/16758221.html