gpt4 book ai didi

delphi - 简单编码解码对象

转载 作者:行者123 更新时间:2023-12-03 15:31:36 25 4
gpt4 key购买 nike

json 支持是 delphi 2009 和 delphi 2010 的新功能之一。我想知道是否有任何简单的函数可以像 super 对象库中那样直接在字符串和对象之间进行编码/解码。

示例:

MyKnownObject := FromJSON('{name:"francis", surname:"lee"}');

最佳答案

参见here 。以下是有趣的部分:

procedure TForm13.Button4Click(Sender: TObject);
var
LContact: TContact;
oMarshaller: TJSONMarshall;
crtVal: TJSONValue;
begin
LContact:=TContact.Create; //our custom class
LContact.Name:='wings-of-wind.com';
LContact.Age:=20; //fill with some data
oMarshaller:=TJSONMarshal.Create(TJSONConverter.Create); //our engine
try
crtVal:=oMarshaller.Marshal(LContact); //serialize to JSON
Memo1.Text:=crtVal.ToString; //display
finally //cleanup
FreeAndNil(LContact);
FreeAndNil(oMarshaller);
end;
end;

您还可以看到here Adrian Andrei(DataSnap 架构师)提供的一个更复杂的示例以及自定义编码(marshal)处理的示例 here .

关于delphi - 简单编码解码对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2041281/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com