TJSON.JsonToObject 不通过 setter [英] TJSON.JsonToObject do not pass through setters

查看:71
本文介绍了TJSON.JsonToObject 不通过 setter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm with some problems when converting a Json string into a Object of my own. I'll give some example:

我的班级:

  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" }');

这意味着当方法JsonToObject"被执行时,他将实例化我的类并将值设置为then,通过setter.属性nome"应该具有值testing:abc",但它只有来自json的abc"部分.调试也不通过设置器.

This means that when the method "JsonToObject" is executed, he will instance my class and set the values to then, passing through the setters. The property "nome" it should have the value "testing: abc", but it has only the "abc" part, from the json. Debugging also do not pass through the setters.

我做错了吗?

推荐答案

您可以创建一个新类,例如TJSON_Respond 帮助序列化

You could create a new class e.g. TJSON_Respond to help the serialization

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

cl := TJson.JSONToObject<TJSON_Respond>('{ "id" : 12, "nome" : "abc" }');

这篇关于TJSON.JsonToObject 不通过 setter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆