delphi XE5 JSON与Delphi Object的互换


本文整理自网络,侵删。

 
unit Unit2;

interface

  uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls,
    DBXJSON, DBXJSONReflect;

  type
    TPerson = class(TObject)
    public
      Name: String;
      Password: String;
      Age: Integer;
    end;

    TForm2 = class(TForm)
      Memo1: TMemo;
      procedure FormCreate(Sender: TObject);
    private
      function ObjectToJSON(AData: TObject): TJSONValue;
      function JSONToObject(AJSONValue: TJSONValue): TObject; 
    public                                                    
    end;

  var
    Form2: TForm2;

implementation

  {$R *.dfm}

  function TForm2.JSONToObject(AJSONValue: TJSONValue): TObject;
  var
    lUnMarshal: TJSONUnMarshal;
  begin
    lUnMarshal := TJSONUnMarshal.Create();
    try
      Result := lUnMarshal.Unmarshal(AJSONValue);
    finally
      FreeAndNil(lUnMarshal);
    end;
  end;

  function TForm2.ObjectToJSON(AData: TObject): TJSONValue;
  var
    lMarshal: TJSONMarshal;
  begin
    lMarshal := TJSONMarshal.Create();
    try
      Result := lMarshal.Marshal(AData);
    finally
      FreeAndNil(lMarshal);
    end;
  end;

  procedure TForm2.FormCreate(Sender: TObject);
  var
    lPerson: TPerson;
    lJSONValue: TJSONValue;
  const
    lJSONString: String = '{"type":"Unit2.TPerson","id":1,"fields":{"Name":"Hezihang","Password":"123","Age":23}}';
  begin
    Memo1.Lines.Clear;
    /// Object Convert to JSON
    Memo1.Lines.Add('Object to JSON String');
    Memo1.Lines.Add('--------------------------------------');
    Memo1.Lines.Add('');
    lPerson       := TPerson.Create;
    lPerson.Name    := 'Hezihang';
    lPerson.Password := '123';
    lPerson.Age   := 23;
    lJSONValue       := ObjectToJSON(lPerson);
    FreeAndNil(lPerson);
    Memo1.Lines.Add(lJSONValue.ToString);
    lJSONValue.Free;
    Memo1.Lines.Add('');
    Memo1.Lines.Add('--------------------------------------');
    /// JSON Convert to Object
    Memo1.Lines.Add('');
    Memo1.Lines.Add('JSON String'' To a Class Instance''');
    Memo1.Lines.Add('--------------------------------------');
    Memo1.Lines.Add('');
    lJSONValue := TJSONObject.ParseJSONValue(lJSONString);
    lPerson := JSONToObject(lJSONValue) as TPerson;
    lJSONValue.Free;
    Memo1.Lines.Add('Name: ' + lPerson.Name);
    Memo1.Lines.Add('Password: ' + lPerson.Password);
    Memo1.Lines.Add('Age: ' + IntToStr(lPerson.Age));
    lPerson.Free;
    Memo1.Lines.Add('');
    Memo1.Lines.Add('--------------------------------------');
  end;

end.

来源:http://blog.csdn.net/cmdasm/article/details/12784627

相关阅读 >>

Delphi 带参数的 exit

Delphi获取文件创建时间、文件最后修改时间

Delphi 通过有道接口实现翻译

Delphi exe注入代码

Delphi varchar和nvarchar区别

Delphi 使窗口透明,并透过透明的窗口操作下面的窗口

16 位浮点运行 pascal/Delphi

Delphi 对话框单元

Delphi getmem(mystring,1024);//开辟1024个字节freemem(mystring,1024);//释放

Delphi新的变量的声明方法

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...