關(guān)于TJSONConverters的使用
unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Buttons, Rest.JSON.Types, Rest.JsonReflect; type TForm1 = class(TForm) Memo1: TMemo; procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end; TValueObject = class private FValue: string; FCreateTime: TDateTime; public constructor Create; property Value: string read FValue write FValue; property CreateTime: TDateTime read FCreateTime write FCreateTime; end; var Form1: TForm1; implementation {$R *.dfm} uses REST.Json, System.Generics.Collections; procedure TForm1.FormCreate(Sender: TObject); begin var ce := TConverterEvent.Create(TValueObject, 'FValue'); // 使用此構(gòu)造函數(shù) ce.StringConverter := function(Data: TObject; Field: string): string begin Result := 'haha'; end; TJSONConverters.AddConverter(ce); Memo1.Text := TJson.ObjectToJsonString(TValueObject.Create); end; { TValueObject } constructor TValueObject.Create; begin inherited Create; FValue := 'test value'; FCreateTime := Now; end; end.
結(jié)果: {"value":"haha","createTime":"2024-01-10T17:15:33.588Z"}文章來源:http://www.zghlxwxcb.cn/news/detail-778603.html
注:REST. XXX單元的序列化類感覺不好用, 想把TObjectList<TPerson>序列化沒有好的切處點。文章來源地址http://www.zghlxwxcb.cn/news/detail-778603.html
到了這里,關(guān)于delphi JSON序列化(五)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!