quicklib json
quicklib json
支持FPC和DELPHI。
unit Unit2; /// <author>cxg 2022-6-12</author> interface uses quick.Json.Serializer, 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>; 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; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure FormCreate(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.FormCreate(Sender: TObject); begin Memo1.Clear; end; end.
本文来自博客园,作者:{咏南中间件},转载请注明原文链接:https://www.cnblogs.com/hnxxcxg/p/16368471.html