如何序列化TList<;T>;不带“的Json;“垃圾”;
我需要将这个json sting序列化为一个Delphi类如何序列化TList<;T>;不带“的Json;“垃圾”;,json,delphi,generics,delphi-xe7,tlist,Json,Delphi,Generics,Delphi Xe7,Tlist,我需要将这个json sting序列化为一个Delphi类 { “主人”:{ “版本”:“1.0” }, “详情”:[ { “idColisEntreeDetail”:0, “codeBarre”:“123456789” }, { “idColisEntreeDetail”:0, “codeBarre”:“234567890” } ] } 这是我的班级: unit unit2; interface uses Generics.Collections, Rest.Json; type
{
“主人”:{
“版本”:“1.0”
},
“详情”:[
{
“idColisEntreeDetail”:0,
“codeBarre”:“123456789”
},
{
“idColisEntreeDetail”:0,
“codeBarre”:“234567890”
}
]
}
这是我的班级:
unit unit2;
interface
uses Generics.Collections, Rest.Json;
type
TDetails = class
private
FCodeBarre: String;
FIdColisEntreeDetail: Extended;
public
property codeBarre: String read FCodeBarre write FCodeBarre;
property idColisEntreeDetail: Extended read FIdColisEntreeDetail
write FIdColisEntreeDetail;
end;
TMaster = class
private
FVersion: String;
public
property version: String read FVersion write FVersion;
end;
TMyClass = class
private
FDetails: TList<TDetails>;
FMaster: TMaster;
public
property Details: TList<TDetails> read FDetails write FDetails;
property Master: TMaster read FMaster write FMaster;
constructor Create;
destructor Destroy; override;
end;
implementation
{ TDetails }
{ TMyClass }
constructor TMyClass.Create;
begin
inherited;
FMaster := TMaster.Create();
end;
destructor TMyClass.Destroy;
var
LDetailsItem: TDetails;
begin
for LDetailsItem in FDetails do
LDetailsItem.free;
FMaster.free;
inherited;
end;
end.
如果改用TArray
类型,这很好,但我会失去所有TList功能
我如何仍然使用TList类型并获得正确的Json输出?很简单,不要使用
TJson.ObjectToJsonString
和TJson.JsonToObject
,它们是垃圾,因为它们只是愚蠢地序列化了每个字段。@StefanGlienke一般来说,通用函数怎么知道需要序列化什么?类的作者需要告诉序列化程序什么需要去,什么不需要去。@davidheffernandelphi已经发布了标记必须序列化的属性的指令。我无法理解为什么在JSON序列化中不使用它作为起点。@DalijaPrasnikar我的代码中有很多这样的示例。@Dalija序列化需要细粒度的控制。属性是前进的方向。