Help JSON Delphi

Asked

Viewed 1,327 times

4

Good afternoon!

Could you help me read this JSON file in Delphi. I have tried several classes, including the native Delphi and I was able to read only the data of the first node, as status. I use the Delphi XE6 version.

{  
   "erro":{  
      "status":400,
      "tipo":"requisicao",
      "causas":[  
         {  
            "codigo":"4210BC95",
            "mensagem":"Banco Caixa Econômica Federal - Campo (boleto.conta.convenio) - O formato do convênio é de até 7 dígitos (6  dígitos + dígito verificador) e deve ser (dddddd-d), onde (d) = numérico, representa um dígito numérico de zero a n ove, mas o informado foi (65).",
        "suporte":"http://www.boletocloud.com/app/dev/api"
     },
     {  
        "codigo":"980D273F",
        "mensagem":"Banco Caixa Econômica Federal - Campo (boleto.conta.agencia) - O formato da agência de 4 dígitos deve ser (dddd), mínimo 1 dígito e máximo 4 dígitos, onde (d) = numérico, representa um dígito numérico de zero a nove, mas o informado foi (45125).",
           "suporte":"http://www.boletocloud.com/app/dev/api"
         }
     ]
   }
}
  • How is your code?

  • Help yourself to these two links... http://answall.com/questions/89379/como-ler-um-ficheiro-json-usando-o-delphi http://balaiotecnologico.blogspot.com.br/2013/07/lendo-dados-json-em-aplicacoes-delphi.html

  • I made several codes with several JSON classes. I had already seen this tutorial, but I didn’t understand the concept of array node. Thank you!

1 answer

3


I created a project as an example. I used Delphi Seattle but I think it works on X6 as well. See below:

Add to uses:

System.JSON

Add a TMemo and put in it the text JSON you passed on your question. Add a TButton and put the code down on OnClick

procedure TForm1.Button1Click(Sender: TObject);
var
    JSonObjectAsString: string;
    JSObj             : TJSONObject;
    JSArray           : TJSONArray;
    JSValue           : TJSONValue;
begin
    JSonObjectAsString := Memo1.Text;

    JSObj := TJSONObject.ParseJSONValue(JSonObjectAsString) as TJSONObject;
    try
        // Erro
        Memo1.Lines.Add(JSObj.GetValue('erro').ToJSON);
        // Status
        Memo1.Lines.Add((JSObj.GetValue('erro') as TJSONObject).GetValue('status').ToJSON);
        // tipo
        Memo1.Lines.Add((JSObj.GetValue('erro') as TJSONObject).GetValue('tipo').ToJSON);
        // causas
        JSArray := (JSObj.GetValue('erro') as TJSONObject).GetValue('causas') as TJSONArray;
        for JSValue in JSArray do
        begin
            // código
            Memo1.Lines.Add((JSValue as TJSONObject).GetValue('codigo').ToJSON);
            // mensagem
            Memo1.Lines.Add((JSValue as TJSONObject).GetValue('mensagem').ToJSON);
            // suporte
            Memo1.Lines.Add((JSValue as TJSONObject).GetValue('suporte').ToJSON);
        end;
    finally
        JSObj.DisposeOf;
    end;
end;
  • Thanks Daniel. It worked, just have to change the . Tojson that does not have in DELPHI XE6 to Tostring. There is a way to remove double quotes automatically, because I’m getting back to them. The examples I was seeing only used Child(xx). Why did you use Jsvalue for array only?

  • I was able to remove it by doing this. I don’t know if the best way, but it was like this: Memo2.Lines.Add((JSValue as TJSONObject).GetValue('codigo').ToString.Replace('"',''));

  • The elements of a JSONArray may be of any kind JSON. In Delphi the TJSONValue is the father of all kinds of JSON. That’s why I used the TJSONValue and then I made a cast for the known type. In case TJSONObject.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.