简单编组解组对象

发布于 2024-08-17 14:34:29 字数 193 浏览 6 评论 0原文

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

例子:

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

json support is one of the new features of delphi 2009 and delphi 2010. I want to know if it's there any simple function to marshalling/unmarshalling directly between string and object like in superobject library.

Example:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

许一世地老天荒 2024-08-24 14:34:29

请参阅此处。下面是有趣的部分:

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;

您也可以看到 这里 Adrian Andrei(DataSnap 架构师)提供的一个更复杂的示例以及自定义封送处理的示例 此处

See here. Bellow is snipped the interesting part:

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;

Also you can see here a more complicated example by Adrian Andrei (the DataSnap architect) as well as an example of custom marshaling here.

娇纵 2024-08-24 14:34:29

将字符串直接反序列化为 TJSONObject

var
  ConvertFrom: String;
  JSON: TJSONObject;
  StringBytes: TBytes;
  I: Integer;
begin
  ConvertFrom := '{"name":"somebody on SO","age":"123"}';
  StringBytes := TEncoding.ASCII.GetBytes(ConvertFrom);
  JSON := TJSONObject.Create;
  try
    JSON.Parse(StringBytes, 0);
    Assert(JSON.ToString = ConvertFrom, 'Conversion test');
    Memo1.Lines.Add(JSON.ToString);

    for I := 0 to JSON.Size - 1 do
      Memo1.Lines.Add(JSON.Get(I).JsonString.Value + 
         ' : ' + JSON.Get(I).JsonValue.Value);

  finally
    JSON.Free;
  end;
end;

Unserialize a string directly to a TJSONObject

var
  ConvertFrom: String;
  JSON: TJSONObject;
  StringBytes: TBytes;
  I: Integer;
begin
  ConvertFrom := '{"name":"somebody on SO","age":"123"}';
  StringBytes := TEncoding.ASCII.GetBytes(ConvertFrom);
  JSON := TJSONObject.Create;
  try
    JSON.Parse(StringBytes, 0);
    Assert(JSON.ToString = ConvertFrom, 'Conversion test');
    Memo1.Lines.Add(JSON.ToString);

    for I := 0 to JSON.Size - 1 do
      Memo1.Lines.Add(JSON.Get(I).JsonString.Value + 
         ' : ' + JSON.Get(I).JsonValue.Value);

  finally
    JSON.Free;
  end;
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文