The string you have is a perfectly formatted JSON array. Written, as source code, it is more readable (JSON doesn’t care about spaces when not inside double quoted string):
JSONText : String =
'[' +
' {' +
' "data": "18/06/2021",' +
' "dataHora": "18/06/2021 22:08",' +
' "descricao": "Objeto em trânsito - por favor aguarde",' +
' "cidade": "SAO JOSE DOS CAMPOS",' +
' "uf": "SP",' +
' "destino": {' +
' "cidade": "JOSE",' +
' "uf": "SC"' +
' }' +
' },' +
' {' +
' "data": "18/06/2021",' +
' "dataHora": "18/06/2021 17:52",' +
' "descricao": "Objeto em trânsito - por favor aguarde",' +
' "cidade": "SAO JOSE DOS CAMPOS",' +
' "uf": "SP",' +
' "destino": {' +
' "cidade": "CAMPOS",' +
' "uf": "SP"}},' +
' {' +
' "data": "18/06/2021",' +
' "dataHora": "18/06/2021 16:53",' +
' "descricao": "Objeto postado",' +
' "cidade": "SAO JOSE DOS CAMPOS",' +
' "uf": "SP"' +
' }' +
']';
Here is a possible solution to decode it and write it into a TMemo:
procedure TForm1.Button2Click(Sender: TObject);
var
JSONArray : TJSONArray;
ArrayElement : TJSONValue;
ItemData : String;
ItemDataHora : String;
ItemDescricao : String;
ItemCidade : String;
ItemUf : String;
ItemDestino : TJSONValue;
ItemDestinoCidade : String;
ItemDestinoUf : String;
begin
Memo1.Clear;
JSONArray := TJSONObject.ParseJSONValue(JSONText) as TJSONArray;
try
for ArrayElement in JsonArray do begin
ItemData := ArrayElement.GetValue<String>('data');
ItemDataHora := ArrayElement.GetValue<String>('dataHora');
ItemDescricao := ArrayElement.GetValue<String>('descricao');
ItemCidade := ArrayElement.GetValue<String>('cidade');
ItemUf := ArrayElement.GetValue<String>('uf');
ItemDestino := ArrayElement.FindValue('destino');
if Assigned(ItemDestino) then begin
ItemDestinoCidade := ItemDestino.GetValue<String>('cidade');
ItemDestinoUf := ItemDestino.GetValue<String>('uf');
end
else begin
ItemDestinoCidade := '';
ItemDestinoUf := '';
end;
Memo1.Lines.Add(Format('data : %s - dataHora : %s - descricao : %s - cidade : %s - uf : %s - destino : cidade : %s , uf : %s',
[ItemData,
ItemDataHora,
ItemDescricao,
ItemCidade,
ItemUf,
ItemDestinoCidade,
ItemDestinoUf]));
end;
finally
JSONArray.Free;
end;
end;
You asked to write it to a TMemo, but it would probably better to use a TStringGrid or a TTreeView.
1
solved How to organize this raw string in a TMemo in Delphi?