Forum > Portuguese

Pegar itens de objeto dentro do json

(1/3) > >>

Johnny Walker Junior:
Alguém poderia fala-me como poderia resolver a questão de pegar um elemento de outro objeto dentro do json?


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.Button22Click(Sender: TObject);var   Data, Result, Return: TJSONData;   I: Integer;begin   // json validado   Data := GetJSON('{"success":true,"message":"returned sucess","data":"[{\"id\":1, \"cpf\":\"99999890667\", \"name\":\"johnny\"}]"}');    // access violation   //Result := Data.findpath('data').Items[0].FindPath('id');   // consigo recuperar o objeto de data   // {\"id\":1, \"cpf\":\"99999890667\", \"name\":\"johnny\"}]"}   Result := Data.FindPath('data');   try     if Assigned(Result) then     begin        //Return := Result.Items[0].FindPath('id');        Return := Result.Items[0];        if Return <> nil then;        begin           // access violation           //Result.FindPath('id').AsString;           //Return.FindPath('data[0].id').AsString;           Memo1.Clear;           Memo1.Text :=  Result.AsJSON;        end;     end;   finally     Data.Free;   end;end;         


Como vêm, consigo recuperar o objeto data, mas não estou compreendendo pegar os elementos internos a ele e seus respectivos valores.


bye

TRon:
Apologies for replying in English (my Portuguese is non-existent).

First of all there is an issue with your original JSON string, at least when reviewing how you tried to access the individual fields. The data item is 'defined' in the JSON as a string while your code seem to suggest it is an array which contains a single object that has individual items.

On top of that the quotes inside that string are escaped. I suspect that something did go wrong with the conversion of the retrieved JSON converting it to a string.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure dodo;var  JSON, DataArray, DataItem: TJSONData;  id: integer;  cpf: string;  ids: string;begin// comment out wrong escaped JSON string//  JSON := GetJSON('{"success":true,"message":"returned sucess","data":"[{\"id\":1, \"cpf\":\"99999890667\", \"name\":\"johnny\"}]"}');   JSON := GetJSON('{"success":true,"message":"returned sucess","data": [ { "id":1, "cpf": "99999890667", "name":"johnny" } ]}');  try    // retrieve array    DataArray := JSON.FindPath('data');    // retrieve first item in the array    DataItem  := DataArray.Items[0];    // or directly, using:    DataItem  := JSON.FindPath('data[0]');     id := DataItem.Items[0].Value;    cpf := DataItem.Items[1].Value;    // or directly using:    id := JSON.FindPath('data[0].id').Value;    cpf := JSON.FindPath('data[0].cpf').Value;    // to convert:    ids := JSON.FindPath('data[0].id').AsString;  finally    JSON.Free;  end;end; 
PS: Note that even when the items inside the data entry are wrongly defined as a string, that you can work around it by reading the string and feeding that to the GetJSON function again. However, that really isn't how JSON is suppose to work.

Johnny Walker Junior:
Thank you very much friend, as I had been scratching my head about this for some time.
I even thought about using a TJSONArray type, but I couldn't find an example that satisfied this curiosity.
Usually, I use json4delphi in lazarus, but I was learning the native json.


Could you tell-me if exist a setting to avoid json come escaped ?

bye

TRon:

--- Quote from: Johnny Walker Junior on April 27, 2024, 03:27:48 pm ---Could you tell-me if exist a setting to avoid json come escaped ?

--- End quote ---
How did you retrieve that JSON string ? e.g. where does it originates from ?

If the JSON string is a result of some use of API (whether it be local or internet) then something did go wrong with the use of that API.

In case it is you yourself who created that string (using fpjson or otherwise) then the string was (also) not generates as JSON intended it to be.

Ergo, without knowing the origin of the JSON string it is hard to tell what can be done to actually try and 'fix' it.

Johnny Walker Junior:
I use Horse framework to do the API.
From this link:
https://github.com/HashLoad/horse

Navigation

[0] Message Index

[#] Next page

Go to full version