Recent

Author Topic: JSON - get field name  (Read 1422 times)

riccardo_cowboy

  • Newbie
  • Posts: 3
JSON - get field name
« on: January 15, 2023, 02:51:24 pm »
Hi to everyone
 From one online service I get JSON data according to following schema:

{
  "error" : [
  ],
  "result" : {
    "NAME1" : "10",
    "NAME2" : "20",
    "NAME3" : "30"
     ........
  }
}

I can read numerical value by using
----------
  jData:=GetJSON(Memo1.Text);
  jArray:=TJSONArray(jData.FindPath('result'));
  for i:=0 to Pred(jArray.Count) do
  begin
     AppStr:=jarray.items.AsString; //get result 10,20,30...
  end;
------

but I need read also the first value (NAME1 etc.): is there one easy way to get it ?.

Thanks
   Riccardo

jamie

  • Hero Member
  • *****
  • Posts: 7493
Re: JSON - get field name
« Reply #1 on: January 15, 2023, 04:10:53 pm »
Please paste your code within the Code blocks, its that little "#" just above this editor. There it will property display what you have for code. :o
The only true wisdom is knowing you know nothing

riccardo_cowboy

  • Newbie
  • Posts: 3
Re: JSON - get field name
« Reply #2 on: January 15, 2023, 05:34:27 pm »
Please paste your code within the Code blocks, its that little "#" just above this editor. There it will property display what you have for code. :o

Jamie
 Tanks for the reply but really I don't think I've underestood your post. I attach complete procedure code.
Can you explain better your meaning?.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.ProcessJSON_GetAccountBalance;
  2. var
  3.   jData:TJSONData;
  4.   jArray:TJSonArray;
  5.   i,j:Integer;
  6.   AppStr:String;
  7. begin
  8.   jData:=GetJSON(Memo1.Text);
  9.   jArray:=TJSONArray(jData.FindPath('result'));
  10.   for i:=0 to Pred(jArray.Count) do
  11.   begin
  12.      AppStr:=jarray.items[i].AsString;
  13.      Memo3.Lines.Add(AppStr);
  14.   end;
  15.   jData.Free;
  16. end;
  17.  

Thanks
  Riccardo

dseligo

  • Hero Member
  • *****
  • Posts: 1651
Re: JSON - get field name
« Reply #3 on: January 16, 2023, 02:25:25 am »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.ProcessJSON_GetAccountBalance;
  2. var
  3.   jData:TJSONData;
  4.   jArray:TJSonArray;
  5.   i,j:Integer;
  6.   AppStr:String;
  7. begin
  8.   jData:=GetJSON(Memo1.Text);
  9.   jArray:=TJSONArray(jData.FindPath('result'));
  10.   for i:=0 to Pred(jArray.Count) do
  11.   begin
  12.      AppStr:= TJSONObject(jarray).Names[i] + ': ' + jarray.items[i].AsString;
  13.      Memo3.Lines.Add(AppStr);
  14.   end;
  15.   jData.Free;
  16. end;
  17.  

riccardo_cowboy

  • Newbie
  • Posts: 3
Re: JSON - get field name
« Reply #4 on: January 17, 2023, 10:50:22 pm »
 TJSONObject(jarray).Names

Now is working   :D
    Thank you !

Ciao

sysrpl

  • Sr. Member
  • ****
  • Posts: 315
    • Get Lazarus
Re: JSON - get field name
« Reply #5 on: January 18, 2023, 05:40:21 pm »
Here is how it's done using JsonTools.

https://github.com/sysrpl/JsonTools

Code: Pascal  [Select][+][-]
  1. uses
  2.   JsonTools;
  3.  
  4. procedure TForm1.Button1Click(Sender: TObject);
  5. const
  6.   Data = '{' +
  7.     '"error" : [' +
  8.     '],' +
  9.     '"result" : {' +
  10.     '  "NAME1" : "10",' +
  11.     '  "NAME2" : "20",' +
  12.     '  "NAME3" : "30"' +
  13.     '}' +
  14.     '}';
  15. var
  16.   Node, Child: TJsonNode;
  17. begin
  18.   Node := TJsonNode.Create;
  19.   try
  20.     Node.Parse(Data);
  21.     for Child in Node.Find('result') do
  22.       WriteLn(Child.Value);
  23.   finally
  24.     Node.Free;
  25.   end;
  26. end;

dseligo

  • Hero Member
  • *****
  • Posts: 1651
Re: JSON - get field name
« Reply #6 on: January 19, 2023, 02:04:06 am »
Here is how it's done using JsonTools.

https://github.com/sysrpl/JsonTools

Of course, you mean:
Code: Pascal  [Select][+][-]
  1.       WriteLn(Child.Name); // not .Value
  2.  

 

TinyPortal © 2005-2018