Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rest TJSON.JsonToObject不通过setter_Rest_Oop_Delphi_Pascal_Setter - Fatal编程技术网

Rest TJSON.JsonToObject不通过setter

Rest TJSON.JsonToObject不通过setter,rest,oop,delphi,pascal,setter,Rest,Oop,Delphi,Pascal,Setter,在将Json字符串转换为我自己的对象时,我遇到了一些问题。我将举一些例子: 我的班级: TClasse = class private Fid: integer; Fnome: string; procedure Setid(const Value: integer); procedure SetNome(const Value: string); published property id : integer read Fid write S

在将Json字符串转换为我自己的对象时,我遇到了一些问题。我将举一些例子:

我的班级:

  TClasse = class
  private
    Fid: integer;
    Fnome: string;
    procedure Setid(const Value: integer);
    procedure SetNome(const Value: string);
  published
    property id : integer read Fid write Setid;
    property nome : string read Fnome write SetNome;
  end;

implementation

procedure TClasse.SetNome(const Value: string);
begin
  Fnome := Value;
  Fnome := 'testing: '+Fnome;
end;
我使用这种方法:

  cl := TJSON.JsonToObject<TClasse>('{ "id" : 12, "nome" : "abc" }');
cl:=TJSON.JsonToObject('{“id”:12,“nome”:“abc”}');
这意味着当执行方法“JsonToObject”时,他将实例化我的类,然后通过setter将值设置为。属性“nome”应该具有值“testing:abc”,但它只有json中的“abc”部分。调试也不会通过setter


我做错什么了吗?

您可以创建一个新类,例如TJSON\u response来帮助序列化

TJSON_Respond= class
  public
    [JSONName('id')] id: Integer;
    [JSONName('nome')] nome: String;
  end;

cl := TJson.JSONToObject<TJSON_Respond>('{ "id" : 12, "nome" : "abc" }');
TJSON\u Respond=class
公众的
[JSONName('id')]id:Integer;
[JSONName('nome')]nome:String;
结束;
cl:=TJson.JSONToObject('{“id”:12,“nome”:“abc”}');

是的,你做错了。。。使用#$%&???Delphi内置JSON库,从第一天起就被破坏了。。。长话短说,它使用字段而不是属性进行序列化,现在我不知道是否可以调整它以使用setter。据报道,当序列化到JSON和从JSON序列化时,您可能需要使用定制的POD类型。