Il 29/12/2022 09:51, David Lastrucci ha scritto:
> Ciao Daniele,
>
> Se la versione Delphi che stai utilizzando è abbstanza recente hai la
> unit System.JSON.
>
> Fai il parse del JSON:
>
> LJSONValue := TJSONObject.ParseJSonValue('{"money": null, "sms": [...]}');
>
> Leggi i valori:
>
> LJSONValue.GetValue<T>('money', nil);
>
> Trovi la documentazione qui:
>
>
https://docwiki.embarcadero.com/Libraries/Sydney/en/System.JSON
>
> Buon anno,
> David
>
>
Grazie David per il suggerimento.
Ora, sempre partendo dall'esempio che ti mandato, ho un nuovo problemino.
Com'e' possibile recuperare gli oggetti e gli array compresi nella
stringa json?
con questo codice (funziona pe ril mio scopo)
procedure ParseJson(StrJson : string);
var
S : string;
LJsonArr : TJSONArray;
LJsonValue : TJSONValue;
LItem : TJSONValue;
jo, jo1 : TJSONObject;
myarr : TJSONArray;
I : Integer;
begin
Form1.Memo1.lines.add('');
jo := TJSONObject.ParseJSONValue(StrJson) as TJSONObject;
try
if jo.TryGetValue('sms', myarr) and (myarr.Count > 0) then
for I := 0 to myarr.Count -1 do
begin
if (myarr.Items[I] is TJSONObject) then
Begin
jo1:= (myarr.Items[I] as TJSONObject);
Form1.Memo1.lines.add(jo1.GetValue('type').ToString +
#32 + ':' + jo1.GetValue('quantity').ToString);
End;
end;
finally
jo.Free;
end;
end;
trovo il valore "sms" che conosco.
Ma se non conosco "sms" com'e' possibile ricavarlo?
Ed infine com'e' possibile rendere recursiva la lettura della stringa
json quado ci sono piu' oggetti che inglobano, magari, piu' array?
Scusami se non soo troppo chiao ...
Buon anno a tutti
Daniele