Recent

Author Topic: How to get the keyname of TJsonData.Index (used with FindPath)  (Read 2536 times)

marque1968

  • Newbie
  • Posts: 3
I have an issue which is probably easy to solve, but I am not seeing the solution...

excerpt of the json file:
Code: Text  [Select][+][-]
  1. {
  2.   "screens": {
  3.     "main": {
  4.       "name": "BuildScreen",
  5.       "border": 0,
  6.       "popup": false,
  7.       "buttons": {
  8.         "num0": {
  9.           "caption": "0",
  10.           "displayposition": "center",
  11.         },
  12.         "info": {
  13.           "caption": "More|Info",
  14.           "displayposition": "center",
  15.         }
  16.       }
  17.     },  

I want to iterate through my buttons. In my test-setup I created:

Code: Pascal  [Select][+][-]
  1.  memo3.text := j.FindPath('screens.main.buttons').Items[iFieldNr].AsJSon;

This gets me all the data needed, except of the ID of the buttons ("num0" and "info" respectively)

I cannot find the way to retrieve this key. So my hope is on the community here. Hope any of you can help me out here.

[Edited to add code tags - please see How to use The Forum.]
« Last Edit: March 08, 2021, 08:38:09 am by trev »

avk

  • Hero Member
  • *****
  • Posts: 752
Re: How to get the keyname of TJsonData.Index (used with FindPath)
« Reply #1 on: March 08, 2021, 08:46:07 am »
TJSONObject has property Names[Index : Integer] : TJSONStringType. This seems to be what you need.

marque1968

  • Newbie
  • Posts: 3
Re: How to get the keyname of TJsonData.Index (used with FindPath)
« Reply #2 on: March 09, 2021, 08:05:16 am »
Thank you@avk,

However it is TJSONdata and this does not have a names option:

Code: [Select]
procedure TForm1.Button4Click(Sender : TObject);
begin
  memo3.text := j.FindPath('screens.main.buttons').Items[1].AsJSon;
  label1.caption := j.FindPath('screens.main.buttons').Names[1];
end;

Findpath is also a TJSONdata type, which does contains the [bItems[/b], but not a Names. The above code therefore does not compile. :(

Perhaps I am missing a way to convert it to TJsonObject?

marque1968

  • Newbie
  • Posts: 3
Re: How to get the keyname of TJsonData.Index (used with FindPath)
« Reply #3 on: March 09, 2021, 08:20:46 am »
Thank you @avk,

As a result of my last remark, I started searching, and found the solution to be rather easy (as long as you know it) :)

Code: [Select]
procedure TForm1.Button4Click(Sender : TObject);
begin
  memo3.text := j.FindPath('screens.main.buttons').Items[1].AsJSon;
  label1.caption := TJSONobject(j.FindPath('screens.main.buttons')).Names[1];
end;

This works. Thank you for steering me in the right direction.

avk

  • Hero Member
  • *****
  • Posts: 752
Re: How to get the keyname of TJsonData.Index (used with FindPath)
« Reply #4 on: March 09, 2021, 07:11:42 pm »
You are welcome.

 

TinyPortal © 2005-2018