fpc序列objectlist为json

fpc序列objectlist为json

unit Unit1;

{$mode delphi}

interface

uses fpjson, fpjsonrtti, Contnrs, //Generics.Collections,
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;

type

  { Ttest }

  Ttest = class
  private
    fid: string;
  published
    property id: string read fid write fid;
  end;

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private

  public

  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
var s: TJSONStreamer;
  items: tobjectlist;
  item: Ttest;
begin
  s := TJSONStreamer.Create(nil);
  items := tobjectlist.Create;
  item := ttest.Create;
  item.id:='中';
  items.Add(item);
  item := ttest.Create;
  item.id:='国';
  items.Add(item);
  memo1.Text:=s.ObjectToJSONString(items);  //{ "Objects" : [{ "id" : "中" }, { "id" : "国" }] }
  s.Free;
  items.Free;
end;

end.

  

posted @ 2023-09-07 16:50  delphi中间件  阅读(97)  评论(0编辑  收藏  举报