You are not logged in.
Pages: 1
Hi to all,
i'm new of this framework, i come from SuperObject and XSuperObject libraries to create my JSON structures.
I have a original TStringList that i want to convert to JSON, using the '.' to start a new sublevel, for example :
if i have :
FIRST=10
TEST.NAME=ccc
TEST.AGE=40
TEST.COUNTRY.NAME=TE
i want to obtain :
{'FIRST':'10', 'TEST':{'NAME':'ccc', 'AGE':'10', 'COUNTRY':{'NAME':'TE'}}
In the past used the SuperOject to obtain this, without any problem. Now i wanto to use the MORMOT to have this.
I write this procedure :
procedure TSapConnection.EstraiStruttura(var Oggetto: Variant; Nome: String; Valore: Variant);
var
Temp: Variant;
Tabella: String;
begin
Temp := _Obj([]);
if Pos('.', Nome) > 0 then
begin
Tabella := Copy(Nome, 1, Pos('.', Nome) - 1);
if Oggetto.Exists(Tabella) then
begin
EstraiStruttura(Temp, Copy(Nome, Pos('.', Nome) + 1, Length(Nome)), Valore);
TDocVariantData(Oggetto).Value[Tabella].Add(Temp); //// <<<--
end
else
begin
EstraiStruttura(Temp, Copy(Nome, Pos('.', Nome) + 1, Length(Nome)), Valore);
Oggetto.Add(Tabella, Temp);
end;
end
else
Oggetto.Add(Nome, Valore);
end;
This produce a json like this :
{'FIRST':'10', 'TEST':{'AGE':'10', 'COUNTRY':{'NAME':'TE'}}
and not
{'FIRST':'10', 'TEST':{'NAME':'ccc', 'AGE':'10', 'COUNTRY':{'NAME':'TE'}}
I think that the problem is on the line with "<<<-- ", i want to add to a "TEST" object another key and value, using the Temp (i.e. a JSON) Variable.
Is it possible? How, if i have an object, can i add a couple value (or values..)?
I tried the
Oggetto._(Tabella).Add(Temp);
But doesn't work.. Can anyone help me?
Thanks.
Offline
I solved... I read again the documentation and solved... Great library and documentation.
The solution is the _safe keywork.
so :
procedure TSapConnection.EstraiStruttura(var Oggetto: Variant; Nome: String; Valore: Variant);
var
Temp: Variant;
Tabella: String;
begin
Temp := _Obj([]);
if Pos('.', Nome) > 0 then
begin
// creiamo un oggetto atto a contenerlo...
Tabella := Copy(Nome, 1, Pos('.', Nome) - 1);
if Oggetto.Exists(Tabella) then
begin
EstraiStruttura(Temp, Copy(Nome, Pos('.', Nome) + 1, Length(Nome)), Valore);
// TDocVariantData(Oggetto).Value[Tabella].Add(Temp);
_safe(_safe(Oggetto).Value[Tabella]).AddFrom(Temp); // Here the correction!
end
else
begin
EstraiStruttura(Temp, Copy(Nome, Pos('.', Nome) + 1, Length(Nome)), Valore);
Oggetto.Add(Tabella, Temp);
end;
end
else
// altrimenti restituiamo quello che abbiamo...
Oggetto.Add(Nome, Valore);
end;
Thanks AB for this wonderful framework.
Merry Christmas.
Offline
Pages: 1