Trafegar json dataSnap

Delphi

16/04/2015

Olá,

como faço para trafegar objetos json entre um cliente e um servidor dataSnap?

no cliente serializo um objeto da seguinte maneira:

var
oMeuObjeto: TMeuObjeto;
oJson: TJSONMarshal;
oValores: TJSONValue;
sUrl: String;
sJson: String;
begin
oMeuObjeto := TMeuObjeto.Create;
oJson := TJSONMarshal.Create(TJSONConverter.Create);
try
oMeuObjeto.ID := StrToIntDef(edId.Text, 0);
oMeuObjeto.Nome := edNome.Text;
oValores := oJson.Marshal(oMeuObjeto);

sJson := oValores.ToString;
sUrl := 'http://localhost:8080/datasnap/rest/TMinhaClasse/LerDadosJsonValue/';
sUrl := sUrl + sJson;
IdHTTP.Get(sUrl);
finally
FreeAndNil(oMeuObjeto);
FreeAndNil(oJson);
end;
end;



e no servidor tenho:
function TMinhaClasse.LerDadosJsonValue(const poMeuObjeto: TJSONObject): String;
var
oJson: TJSONUnMarshal;
oMeuObjeto: TMeuObjeto;
oObjeto: TObject;
oJSONValue: TJSONObject;
begin
oJSONValue := poMeuObjeto;
oJson := TJSONUnMarshal.Create;
try
oObjeto := oJson.Unmarshal(oJSONValue);
try
if (oObjeto is TMeuObjeto) then
begin
oMeuObjeto := TMeuObjeto(oObjeto);
Result := oMeuObjeto.Nome;
end;
finally
FreeAndNil(oObjeto);
end;
finally
freeAndNil(oJson);
end;
end;


o problema é quando chega nesta linha:
oObjeto := oJson.Unmarshal(oJSONValue);

apresenta a seguinte mensagem:
Project Server.exe raiser exception class EConversionError with message "The input values is not a valid Object'
Luiz Sa

Luiz Sa

Curtidas 0

Respostas

Anderson Amaral

Anderson Amaral

16/04/2015

Olá Luiz Eduardo, já tive a mesma mensagem ao tentar transformar uma string JSON para um objeto. Verifica se não existe na string algum caractere fora do contexto, as vezes até mesmo colchetes do array pode dar este tipo de problema dependendo da conversão que estás fazendo. Exemplo:

[{"id":1,"campo2":"AAAA"}]

tente assim:
{"id":1,"campo2":"AAA"}

Espero que contribua de alguma forma com a solução.
GOSTEI 0
POSTAR