How to read a json file using Delphi

Asked

Viewed 10,995 times

5

1 answer

3

Here’s an example of how you make the Delphi call to your url and get the json values

function TForm3.getTemp: TTemp;
var
  lHTTP: TIdHTTP;
  lParamList: TStringList;

   LJSONObject : TJSONObject;

   j:integer;
   jSubPar: TJSONPair;


    jsonStringData : String;
begin



    // chamada a URL
    lParamList := TStringList.Create;
    lHTTP := TIdHTTP.Create(nil);
   try
      jsonStringData := lHTTP.Post('http://www.nif.pt/?json=1&q=509442013', lParamList);
   finally
     lHTTP.Free;
     lParamList.Free;
   end;




   //  obtendo valores
   LJSONObject := nil;
   try

      LJSONObject := TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(jsonStringData), 0) as TJSONObject;


      for j := 0 to LJSONObject.Size - 1 do  begin
         jSubPar := LJSONObject.Get(j);  //pega o par no índice j
         if jSubPar.JsonString.Value = 'data' then begin
            jsonStringData :=  jSubPar.toString;
         end;
      end;


      LJSONObject := nil;
      LJSONObject := TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(jsonStringData.Replace('"data":','',[rfReplaceAll])), 0) as TJSONObject;

      Result := TTemp.Create;
      for j := 0 to LJSONObject.Size - 1 do  begin

        // NOME DO CAMPO
        jSubPar.JsonString.Value


        // VALOR
        Result.location := jSubPar.JsonValue.Value


            // {"result":"success"
        if (trim(jSubPar.JsonString.Value) = 'result') then
            jSubPar.JsonValue.Value // RETORNO success

      end;


   finally
      LJSONObject.Free;
   end;
end;

I also made another function to return the value of a specific field:

function TForm3.getCamposJsonString(json, value:String): String;
var
 LJSONObject: TJSONObject;
  jSubPar: TJSONPair;
   i,j:integer;
begin

   LJSONObject := nil;
   try

      LJSONObject := TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(json),0) as TJSONObject;


      for j := 0 to LJSONObject.Size - 1 do  begin
         jSubPar := LJSONObject.Get(j);  //pega o par no índice j
         if (trim(jSubPar.JsonString.Value) = value) then
            Result :=   jSubPar.JsonValue.Value;

      end;
   finally
      LJSONObject.Free;
   end;
end;
  • i wanted to show this on a grid or a label for example address

  • just you ride a stringGrid inside the for, where you have the comments field and value.

  • I have a button

  • I don’t know how to use functions yet I didn’t get there...java is more friendly...

  • 1

    Putz, then you are in trouble. kkkkkk. I think you better study a little more about the language to get more familiar.

  • the value of the second function is what?

  • I’m trying the second function but always jumps to the end not le the fields of my

  • It’s the name of the field you want to get from your json.

  • hum . but I can’t get the nif...

  • So, I gave you an idea of how to access a json, its fields and its values. All you have to do now is adjust the methods I gave you to your reality.

  • I’m wearing the x2 deplhi

Show 6 more comments

Browser other questions tagged

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