Fórum Converter json em record #601477
27/03/2019
0
Pessoal
Estou precisando converter um json em um record
alguém tem alguma sugestão!??
agradeco desde já
Ricardo Matarazzo
Curtir tópico
+ 0Posts
27/03/2019
Artur Barth
Sugiro utilizar essa biblioteca: https://github.com/fabriciocolombo/delphi-rest-client-api
Exemplo de código:
TPerson = class(TObject)
public
(* Reflect the server side object field names, for Java must be case-sensitive *)
id: Integer;
name: String;
email: String;
(* Static constructor *)
class function NewFrom(Id: Integer; Name, EMail: String): TPerson;
end;
var
vPerson : TPerson;
begin
vPerson := RestClient.Resource('http://localhost:8080/java-rest-server/rest/person/1')
.Accept(RestUtils.MediaType_Json)
.Get<TPerson>();
Se preferir, você pode utilizar uma chamada lkJSON: https://sourceforge.net/projects/lkjson/
Não tenho exemplo com lkJSON.
Ou, até mesmo as funções nativas do Delphi que estão disponíveis através da uses Rest.JSON a partir da versão XE se não me engano.
{==========================================
Converte JSON para Objeto
==========================================}
class function TJsonUtil.JSONToObject<T>(AJSON: TJSONValue): T;
begin
if AJSON is TJSONNull then
Exit(nil);
if not Assigned(JSONUnMarshal) then
JSONUnMarshal := TJSONUnMarshal.Create;
result := T(JSONUnMarshal.UnMarshal(AJSON));
end;
Onde T é generics, ou seja, suporta qualquer classe ou objeto que você passe por parâmetro e gerará um objeto a partir do JSON.
Gostei + 0
28/03/2019
Ricardo Matarazzo
Sugiro utilizar essa biblioteca: https://github.com/fabriciocolombo/delphi-rest-client-api
Exemplo de código:
TPerson = class(TObject)
public
(* Reflect the server side object field names, for Java must be case-sensitive *)
id: Integer;
name: String;
email: String;
(* Static constructor *)
class function NewFrom(Id: Integer; Name, EMail: String): TPerson;
end;
var
vPerson : TPerson;
begin
vPerson := RestClient.Resource('http://localhost:8080/java-rest-server/rest/person/1')
.Accept(RestUtils.MediaType_Json)
.Get<TPerson>();
Se preferir, você pode utilizar uma chamada lkJSON: https://sourceforge.net/projects/lkjson/
Não tenho exemplo com lkJSON.
Ou, até mesmo as funções nativas do Delphi que estão disponíveis através da uses Rest.JSON a partir da versão XE se não me engano.
{==========================================
Converte JSON para Objeto
==========================================}
class function TJsonUtil.JSONToObject<T>(AJSON: TJSONValue): T;
begin
if AJSON is TJSONNull then
Exit(nil);
if not Assigned(JSONUnMarshal) then
JSONUnMarshal := TJSONUnMarshal.Create;
result := T(JSONUnMarshal.UnMarshal(AJSON));
end;
Onde T é generics, ou seja, suporta qualquer classe ou objeto que você passe por parâmetro e gerará um objeto a partir do JSON.
Gostei + 0
28/03/2019
Ricardo Matarazzo
Sugiro utilizar essa biblioteca: https://github.com/fabriciocolombo/delphi-rest-client-api
Exemplo de código:
TPerson = class(TObject)
public
(* Reflect the server side object field names, for Java must be case-sensitive *)
id: Integer;
name: String;
email: String;
(* Static constructor *)
class function NewFrom(Id: Integer; Name, EMail: String): TPerson;
end;
var
vPerson : TPerson;
begin
vPerson := RestClient.Resource('http://localhost:8080/java-rest-server/rest/person/1')
.Accept(RestUtils.MediaType_Json)
.Get<TPerson>();
Se preferir, você pode utilizar uma chamada lkJSON: https://sourceforge.net/projects/lkjson/
Não tenho exemplo com lkJSON.
Ou, até mesmo as funções nativas do Delphi que estão disponíveis através da uses Rest.JSON a partir da versão XE se não me engano.
{==========================================
Converte JSON para Objeto
==========================================}
class function TJsonUtil.JSONToObject<T>(AJSON: TJSONValue): T;
begin
if AJSON is TJSONNull then
Exit(nil);
if not Assigned(JSONUnMarshal) then
JSONUnMarshal := TJSONUnMarshal.Create;
result := T(JSONUnMarshal.UnMarshal(AJSON));
end;
Onde T é generics, ou seja, suporta qualquer classe ou objeto que você passe por parâmetro e gerará um objeto a partir do JSON.
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)