-3

I have a very dirty string here and I need a code in Delphi that organize it. My program always get this dirty strings, look one.

[{"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"}]

I need a little help to put this words in order, just like:

 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 : CAMPOS , uf : SP 
 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 

and still going on. What is the fastest way to make it?

2
  • 4
    That is not a "dirty" string. It is JSON. Delphi has a builtin JSON framework, start with TJSONObject.ParseJSONValue() and go from there. Why do you want to put this into a TMemo, though? A more structured control like a TListView or TTreeView might make more sense. Commented Jun 19, 2021 at 4:28
  • I wonder where you get "this dirty strings" from and why the source doesn't tell you straight away that it is supposed to be JSON. Are you sure you don't miss at least one important fact? As per the data you and this guy work together.
    – AmigoJack
    Commented Jun 20, 2021 at 10:28

1 Answer 1

3

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
  • Dude, you saved my day! Thank you, take the green tick! You are a wizard! Commented Jun 19, 2021 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.