Delphi7实现Json对象的序列化与反序列化

在高版本的 Delphi 中,实现序列化和反序列化非常简单。然而,在 Delphi 7 中,这个过程仍然需要一些额外的努力。为了简化这个问题,我花了一些时间封装了一个支持序列化和反序列化的 JSON 解析库。

javascript 复制代码
type
  {$M+}
  TStartupParameters = class
  private
    FAccount: String;
    FPassword: String;
    FServerName: String;
    FChannel: String;
    FTaskName: String;
    FWindowHandle: HWND;
  public
    constructor Create;
    destructor Destroy; override;
  published
    property Account: String read FAccount write FAccount;
    property Password: String read FPassword write FPassword;
    property ServerName: String read FServerName write FServerName;
    property Channel: String read FChannel write FChannel;
    property TaskName: String read FTaskName write FTaskName;
    property WindowHandle: HWND read FWindowHandle write FWindowHandle;
  end;
  {$M-}
  
function ObjectToJson: string;
var
   obj: ISuperObject;
   LStartupParameters: TStartupParameters;
begin
   LStartupParameters := TStartupParameters.Create;
   LStartupParameters.FAccount:= 'test';
   LStartupParameters.FPassword:= '123456';

   obj := TOldRttiMarshal.ToJson(LStartupParameters);
   Result:= obj.AsJson;
end;

function JsonToObject(JsonString: string): string;
begin
   StartupParameters := TStartupParameters(TOldRttiUnMarshal.FromJson(TStartupParameters, JsonString));
   Result:= 'Account: ' + StartupParameters.Account +#13#10+ 'Password: ' + StartupParameters.Password;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Memo1.Lines.Add(ObjectToJson);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  JsonToObject(Memo1.Text);
  Memo2.Lines.Add(JsonToObject(Memo1.Text));
end;
相关推荐
ID_180079054731 天前
Python采集京东店铺所有商品数据,json数据返回
json
ljh5746491191 天前
mysql 必须在逗号分隔字符串和JSON字段之间二选一,怎么选
数据库·mysql·json
小孔龙1 天前
02.Kotlin Serialization 属性序列化控制
kotlin·json
Cachel wood2 天前
信息检索、推荐系统模型排序质量指标:AP@K和MAP@K
windows·搜索引擎·json·推荐系统·搜索
tebukaopu1482 天前
json文件转excel
json·excel
小孔龙3 天前
01.Kotlin Serialization - 基础用法
kotlin·json
佛珠散了一地3 天前
【qt】通过TCP传输json,json里包含图像
qt·tcp/ip·json
Dersun3 天前
python学习进阶之异常和文件操作(三)
开发语言·python·学习·json
晴天下小雨o4 天前
Json-rpc通信项目(基于C++ Jsoncpp muduo库)
c++·rpc·json