泛型数组 + 记录类型 + Json 之间的转换

时间:2025-05-14 20:03:26
unit Unit3;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, System.Generics.Collections; type
TForm3 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; var
Form3: TForm3; /// <summary>
/// 这里注意必须在interface部分声明.
/// </summary>
type TRen = record
name: string;
age: Integer;
end; implementation {$R *.dfm} uses qjson; procedure TForm3.Button1Click(Sender: TObject);
const
MyJson: string = '[{"name":"群主","age":100},{"name":"老衲","age":100}]';
var
MyRenAry: TArray<TRen>;
MyQj: TQJson;
begin
MyQj := TQJson.Create;
try
//---------------------------------------
MyQj.Parse(MyJson);
MyQj.ToRtti(@MyRenAry,TypeInfo(TArray<TRen>)); ShowMessage(MyRenAry[].name); //---------------------------------------
MyQj.FromRtti(@MyRenAry,TypeInfo(TArray<TRen>));
ShowMessage(MyQj.AsJson);
finally
MyQj.Free;
end;
end; end.