频道分类

Delphi 将对象转换为JSON字符串或TJSONObject

作者:admin 来源: 日期:2019/10/29 18:46:38 人气: 标签:

 
type
  TPerson = record
  private
    FAge: Integer;
    FName: string;
  public
    property Name: string read FName write FName;
    property Age: Integer read FAge write FAge;
  end;

uses
  System.Generics.Collections,
  REST.Json;

procedure TForm1.Button1Click(Sender: TObject);
var
  Alice, Bob: TPerson;
  People: TList<TPerson>;
  S: string;
begin
  People := TList<TPerson>.Create;

  Alice.Name := 'Alice';
  Alice.Age := 20;
  People.Add(Alice);
  Bob.Name := 'Bob';
  Bob.Age := 30;
  People.Add(Bob);

  S := TJson.ObjectToJsonString(People);
  Memo1.Text := S;
end;