加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Delphi Rest.JSON JsonToObject仅适用于f变量

发布时间:2020-12-15 04:08:06 所属栏目:大数据 来源:网络整理
导读:我正在使用Delphi XE8. 我只是看着REST.Json ObjectToJsonString()和JsonToObject()调用. 主要是尝试做这样的事情: How to convert an object to JSON and back with a single line of code 我注意到,当他们以F字符开始时,我只能获得变量.我找不到任何关于
我正在使用Delphi XE8.

我只是看着REST.Json ObjectToJsonString()和JsonToObject()调用.

主要是尝试做这样的事情:

How to convert an object to JSON and back with a single line of code

我注意到,当他们以F字符开始时,我只能获得变量.我找不到任何关于此的文件.这是预期的行为吗?我应该在开始时使用F命名我的类中的所有变量吗?如果是,有人可以解释原因吗?

我创建了一个类TTestJSON并定义了两个成员变量并将它们设置为’WORKS’和’FAILS’.

然后我从对象创建了一个JSON字符串值:

{
  "varThatWorksBeacuseItStartsWithF":"WORKS","sVarThatFailsBecauseItStartsWithS":"FAILS"
}

从JSON字符串返回到对象时,只有正确重置fVarThatWorksBeacuseItStartsWithF变量.在下面的代码中,test:= TJson.JsonToObject< TTestJSON>(JsonStr);使用上面的JSON,注意sVarThatFailsBecauseItStartsWithS是“”而不是“FAILS”.

procedure TForm3.btn1Click(Sender: TObject);
var
  test : TTestJSON;
  JsonStr : String;
begin
  m1.Clear;
  test := TTestJSON.Create;
  try
    test.fVarThatWorksBeacuseItStartsWithF := 'WORKS';
    test.sVarThatFailsBecauseItStartsWithS := 'FAILS';
    JsonStr := TJson.ObjectToJsonString( test );
  finally
    test.Free;
  end;
  m1.Lines.Add(  '** JSONStr Value START **' + #13#10 + JsonStr + '** JSONStr Value END **' + #13#10 );

  test := TJson.JsonToObject<TTestJSON>(JsonStr);
  try
    m1.Lines.Add('** Obj loaded from JSON String Start **' + #13#10 + TJson.ObjectToJsonString( test ) + #13#10 + '** Obj loaded from JSON String End **');
  finally
    test.Free;
  end;
end;

从结果中,以f开头的var将f从fSON字符串中剥离出来,而以s开头的那个仍然存在于其中.我原以为第二个结果会是这样的:

{
  "varThatWorksBeacuseItStartsWithF":"WORKS","sVarThatFailsBecauseItStartsWithS":"FAILS"
}

以下是重现的完整代码 – 只需在vcl表单上有一个按钮和一个备忘录 – 也使用REST.Json:

unit Main;

interface

uses
  Winapi.Windows,Winapi.Messages,System.SysUtils,System.Variants,System.Classes,Vcl.Graphics,Vcl.Controls,Vcl.Forms,Vcl.Dialogs,Vcl.StdCtrls,rest.Json;

type
  TTestJSON = class
    fVarThatWorksBeacuseItStartsWithF : String;
    sVarThatFailsBecauseItStartsWithS : String;
  end;

  TForm3 = class(TForm)
    btn1: TButton;
    m1: TMemo;
    procedure btn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form3: TForm3;

implementation

{$R *.dfm}

procedure TForm3.btn1Click(Sender: TObject);
var
  test : TTestJSON;
  JsonStr : String;
begin
  m1.Clear;
  test := TTestJSON.Create;
  try
    test.fVarThatWorksBeacuseItStartsWithF := 'WORKS';
    test.sVarThatFailsBecauseItStartsWithS := 'FAILS';
    JsonStr := TJson.ObjectToJsonString( test );
  finally
    test.Free;
  end;
  m1.Lines.Add(  '** JSONStr Value START **' + #13#10 + JsonStr + '** JSONStr Value END **' + #13#10 );

  test := TJson.JsonToObject<TTestJSON>(JsonStr);
  try
    m1.Lines.Add('** Obj loaded from JSON String Start **' + #13#10 + TJson.ObjectToJsonString( test ) + #13#10 + '** Obj loaded from JSON String End **');
  finally
    test.Free;
  end;
end;

end.

解决方法

Delphi中的JSON序列化基于字段,而不是属性.但是大多数Delphi类都有友好属性和F前缀字段.同时,Emb似乎试图避免生成的JSON中的F前缀名称.在序列化字段时,它们从名称中删除了第一个“F”,并在从JSON读取时将其添加回(以查找正确的字段).似乎在Delphi中使用JSON序列化的唯一(安全)方法是保留所有字段名称前缀为“F”(对于要序列化的字段):
TTestJSON = class
protected
  FName: String;
public
  property Name: String read FName write FName;
end;

更新2:正如大卫提到的,我们可以使用属性,然后我们可以更好地控制:

uses
  REST.Json.Types,// without this unit we get warning: W1025 Unsupported language feature: 'custom attribute'
  REST.Json;

type
  // All fields of records are serialized,no control here.
  TRec = record
    RecStr: String;
  end;

  // By default all fields of class are serialized,but only F-prefixed serialized correctly.
  // We can use JSONMarshalled attribute to enable/disable serialization.
  // We can use JSonName attribute to serialize field with specific name in JSON.
  TTestJSON = class
    [JSONMarshalled(True)] [JSonName('RecField')]
    R: TRec;
  end;

procedure TForm28.FormCreate(Sender: TObject);
var
  Test: TTestJSON;
  JsonStr: string;
begin
  Test := TTestJSON.Create;
  try
    Test.R.RecStr := 'Some str';
    JsonStr := TJson.ObjectToJsonString( Test );
  finally
    FreeAndNil(Test);
  end;

  // JsonStr: {"RecField":["Some str"]}

  Test := TJson.JsonToObject<TTestJSON>(JsonStr);
  FreeAndNil(Test);
end;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读