5
Good not being a pro in delphi
want to read a file json
and extract fields.
5
Good not being a pro in delphi
want to read a file json
and extract fields.
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;
Browser other questions tagged json delphi
You are not signed in. Login or sign up in order to post.
i wanted to show this on a grid or a label for example address
– usersantos
just you ride a
stringGrid
inside the for, where you have the comments field and value.– Eduardo Binotto
I have a button
– usersantos
I don’t know how to use functions yet I didn’t get there...java is more friendly...
– usersantos
Putz, then you are in trouble. kkkkkk. I think you better study a little more about the language to get more familiar.
– Eduardo Binotto
the value of the second function is what?
– usersantos
I’m trying the second function but always jumps to the end not le the fields of my
– usersantos
It’s the name of the field you want to get from your json.
– Eduardo Binotto
hum . but I can’t get the nif...
– usersantos
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.
– Eduardo Binotto
I’m wearing the x2 deplhi
– usersantos