Delphi XE-Rest.JSON,具有相同名称的父子类属性
在下面的示例中,我尝试使用Rest.JSON,但丢失了相同名称的属性parent-child class(Items)。只需在Delphi XE中创建一个新的vcl应用程序并粘贴以下代码即可查看发生的情况:Delphi XE-Rest.JSON,具有相同名称的父子类属性,json,delphi,Json,Delphi,在下面的示例中,我尝试使用Rest.JSON,但丢失了相同名称的属性parent-child class(Items)。只需在Delphi XE中创建一个新的vcl应用程序并粘贴以下代码即可查看发生的情况: unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vc
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Rest.JSON;
type
TParentItemsClass = class
strict private
fID: integer;
fName: string;
public
property ID: integer read fID write fID;
property Name: string read fName write fName;
end;
TParentClass = class
strict private
fID: integer;
fName: string;
fItems: TParentItemsClass;
public
constructor Create;
property ID: integer read FID write fID;
property Name: string read FName write fName;
property Items: TParentItemsClass read FItems write fItems;
end;
TChildItemsClass = class(TParentItemsClass)
strict private
fSomeField: integer;
public
property SomeField: integer read fSomeField write fSomeField;
end;
TChildClass = class(TparentClass)
strict private
fItems: TChildItemsClass;
public
constructor Create;
property Items: TChildItemsClass read fItems write fItems;
end;
TForm1 = class(TForm)
private
{ Private declarations }
public
constructor Create(AOwner: TComponent); override;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TParentClass }
{ TParentClass }
constructor TParentClass.Create;
begin
fItems:= TParentItemsClass.Create;
end;
{ TChildClass }
constructor TChildClass.Create;
begin
fItems:= TChildItemsClass.Create;
end;
{ TForm1 }
constructor TForm1.Create(AOwner: TComponent);
var AChild, BChild: TChildClass;
begin
inherited;
AChild:= TChildClass.Create;
AChild.Items.ID:= 1;
AChild.Items.SomeField:= 2;
AChild.Items.Name:= 'abc';
BChild:= TJSON.JsonToObject<TChildClass>(TJSON.ObjectToJsonString(AChild));
Application.MessageBox(PWideChar(TJSON.ObjectToJsonString(AChild)+#13+#13+TJSON.ObjectToJsonString(BChild)), '', mb_ok);
end;
end.
单元1;
接口
使用
Winapi.Windows、Winapi.Messages、System.SysUtils、System.Variants、System.Classes、Vcl.Graphics、,
控件、窗体、对话框、Rest.JSON;
类型
TParentItemsClass=类
严格保密
fID:整数;
fName:字符串;
公众的
属性ID:整数读取fID写入fID;
属性名称:字符串读取fName写入fName;
结束;
TParentClass=class
严格保密
fID:整数;
fName:字符串;
fItems:TParentItemsClass;
公众的
构造函数创建;
属性ID:整数读取FID写入FID;
属性名称:字符串读取FName写入FName;
属性项:TParentItemsClass读取FItems写入FItems;
结束;
TchildItemClass=类(TParentItemsClass)
严格保密
fSomeField:整数;
公众的
属性SomeField:整数读取fSomeField写入fSomeField;
结束;
TChildClass=类(TparentClass)
严格保密
fItems:TChildItemsClass;
公众的
构造函数创建;
属性项:TchildItemClass读fItems写fItems;
结束;
TForm1=类(TForm)
私有的
{私有声明}
公众的
构造函数创建(AOwner:TComponent);推翻
结束;
变量
表1:TForm1;
实施
{$R*.dfm}
{TParentClass}
{TParentClass}
构造函数TParentClass.Create;
开始
fItems:=TParentItemsClass.Create;
结束;
{TChildClass}
构造函数TChildClass.Create;
开始
fItems:=TChildItemsClass.Create;
结束;
{TForm1}
构造函数TForm1.Create(所有者:TComponent);
var AChild,BChild:TChildClass;
开始
继承;
AChild:=TChildClass.Create;
AChild.Items.ID:=1;
AChild.Items.SomeField:=2;
AChild.Items.Name:=“abc”;
BChild:=TJSON.JsonToObject(TJSON.ObjectToJsonString(AChild));
Application.MessageBox(PWideChar(TJSON.ObjectToJsonString(AChild)+#13+#13+TJSON.ObjectToJsonString(BChild)),'',mb#ok);
结束;
结束。
这导致:
阿基尔德: {“items”:{“someField”:2,“iD”:1,“name”:“abc”},“iD”:0,“name”:“items”:null} B孩子 {“items”:null,“iD”:0,“name”:“”“items”:null}
如何避免这种情况?我需要BChild JSON看起来像AChild JSON,你的类
TParentClass
和TChildClass
都有一个私有字段fItems
。如果您仔细观察代码输出的JSON,您已经在问题中给出了答案
查看AChild的值:
{
“项目”:{
“someField”:2,
“iD”:1,
“名称”:“abc”
},
“iD”:0,
“名称”:“,
“项目”:空
}
两次查看它如何包含项。这就是当您将JSON字符串封送回BChild
时使用的内容。这肯定不是你想要的
一个快速的解决方法是将您的内部字段TChildClass.fItems
重命名为,例如TChildClass.fItems\uu
谢谢您的回答,您帮了我很多忙。我正走进“黑暗之路”来解决这个问题,你的建议拯救了我的一天!