如何在delphi中使数组“序列化”和“反序列化”?

时间:2019-10-17 13:16:49

标签: arrays delphi delphi-7

我需要像在php上一样在delphi上进行“序列化”和“反序列化”,以便将生成的数组存储在站点数据库中。那些。我需要得到类似的东西。

  

a:10:{i:0; a:3:{i:0; s:4:“ Obj1”; i:1; i:383; i:2; i:339;} i:1; a:3:{i:0; s:4:“ Obj2”; i:1; i:339; i:2; i:18;} i:2; a:3:{i:0; s:4 :“ Obj3”; i:1; i:386; i:2; i:98;} i:3; a:3:{i:0; s:4:“ Obj4”; i:1; i:428 ; i:2; i:286;} i:4; a:3:{i:0; s:4:“ Obj5”; i:1; i:54; i:2; i:47;} i: 5; a:3:{i:0; s:4:“ Obj6”;​​ i:1; i:328; i:2; i:27;} i:6; a:3:{i:0; s :4:“ Obj7”; i:1; i:332; i:2; i:371;} i:7; a:3:{i:0; s:4:“ Obj8”; i:1; i :42; i:2; i:187;} i:8; a:3:{i:0; s:4:“ Obj9”; i:1; i:314; i:2; i:68;} i:9; a:3:{i:0; s:5:“ Obj10”; i:1; i:124; i:2; i:120;}}

这是我的代码在delphi中添加对象:


interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, Contnrs, StdCtrls;

type
  TForm1 = class(TForm)
    Image1: TImage;
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 TRect = class 
  private
   p:TPoint;
   width, height: Integer;
   color : TColor;
   str:String;
  public
    constructor Create(APt:TPoint;w1, h1: Integer; _Color:TColor;str1:String);
    Procedure Draw(Cs:TCanvas;x:Integer;y:Integer); 
  end;
   TRectList=class(TObjectList)
  private
    function GetItems(index:integer):TRect;
    procedure SetItems(index:integer; Value:TRect);
  public
    property Items[index:integer]: TRect read GetItems write SetItems; default;
  end;

var
  Form1: TForm1;
  ImPic : TBitmap;
  RectList: TRectList;
  rect : TRect;
  start : TPoint;
  ObjId:Integer;


implementation
{$R *.dfm}
constructor TRect.Create(APt:TPoint; w1, h1: Integer; _Color: TColor;str1:String);
begin
  p:=APt;
  width := w1;
  height := h1;
  color := _Color;
  str:=str1;

end;

procedure TRect.Draw(Cs:TCanvas; x, y: Integer);
begin
  Cs.Brush.Color:=color;
  Cs.Rectangle(p.X,p.Y,p.X+width,p.Y+height);
end;


procedure TForm1.Button1Click(Sender: TObject);
var i,x,y:Integer;
begin
x := Random(500);
y := Random(400);
ObjId := ObjId+1;

 start := Point(x, y);
 rect := TRect.Create(start,20,20,clBlue,'Obj'+IntToStr(ObjId));
 RectList.Add(rect);
 Memo1.Lines.Clear;
 Image1.Canvas.Brush.Color:=clWhite;
 Image1.Canvas.FillRect(Image1.ClientRect);
 Memo1.Lines.Add('[');
 for i := 0 to RectList.Count-1 do
 begin
   RectList[i].Draw(Image1.Canvas,RectList[i].p.X,RectList[i].p.Y);
   Memo1.Lines.Add('["'+RectList[i].str+'",'+IntToStr(RectList[i].p.X)+','+IntToStr(RectList[i].p.Y)+'],');
 end;
  Memo1.Lines.Add(']');
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
RectList := TRectList.Create(true);
randomize();
with Image1.Canvas do
begin
   brush.Color:=clSilver;
   Rectangle(0,0,width,height);
end;
end;
function TRectList.GetItems(index: integer):TRect;
begin
   Result := inherited Items[index] as TRect;
end;

procedure TRectList.SetItems(index: integer; Value: TRect);
begin
   inherited Items[index] := Value;
end;

end.

在“ memo”中生成一个数组。图片中的示例: img 然后,您可以将此数组添加到php文件中,应用序列化,然后获得显示在文章顶部的数组。邮递区号:

$array = [
["Obj1",383,339],
["Obj2",339,18],
["Obj3",386,98],
["Obj4",428,286],
["Obj5",54,47],
["Obj6",328,27],
["Obj7",332,371],
["Obj8",42,187],
["Obj9",314,68],
["Obj10",124,120],
]
;
$string = serialize( $array );
echo $string."<br>";
$array = unserialize($string);
echo "<pre>";
var_dump($array);
echo "</pre>";  
?>

如何在delphi上执行“序列化”和“反序列化”相同的操作,以便随后存储在站点的mysql数据库中?

0 个答案:

没有答案