Recent

Author Topic: How to iterate JSON arrays?  (Read 866 times)

TCH

  • Full Member
  • ***
  • Posts: 200
How to iterate JSON arrays?
« on: June 30, 2020, 07:15:16 pm »
I have the following JSON array:
Code: Javascript  [Select][+][-]
  1. {
  2.         "data":
  3.         {
  4.                 "array":
  5.                 [
  6.                         {
  7.                                 "type1":
  8.                                 {
  9.                                         ...
  10.                                 }
  11.                         },
  12.                         {
  13.                                 "type2":
  14.                                 {
  15.                                         ...
  16.                                 }
  17.                         },
  18.                         {
  19.                                 "type3":
  20.                                 {
  21.                                         ...
  22.                                 }
  23.                         },
  24.                         {
  25.                                 "type3":
  26.                                 {
  27.                                         ...
  28.                                 }
  29.                         },
  30.                         {
  31.                                 "type3":
  32.                                 {
  33.                                         ...
  34.                                 }
  35.                         },
  36.                         {
  37.                                 "type3":
  38.                                 {
  39.                                         ...
  40.                                 }
  41.                         },
  42.                         ...
  43.                 ]
  44.         }
  45. }
I would like to work with the type3 nodes for what i have the following Pascal code:
Code: Pascal  [Select][+][-]
  1. JSON := GetJSON(the_json_above);
  2. TMPJSON := JSON.FindPath('data.array');
  3. l := TMPJSON.Count - 1;
  4. q := 0;
  5. for r := 0 to l do
  6. begin
  7.         TMPJSON1 := TMPJSON.Items[r].FindPath('type3');
  8.         if (TMPJSON1 <> nil) then
  9.         begin
  10.                 inc(q);
  11.                 if (q = 20) then
  12.                 begin
  13.                         break;
  14.                 end;
  15.         end;
  16. end;
This causes crash. The parser finds data.array, as the debugger shows TMPJSON.Count correctly.

How can one iterate the JSON arrays?

alaa123456789

  • Sr. Member
  • ****
  • Posts: 260
  • Try your Best to learn & help others
    • youtube:
Re: How to iterate JSON arrays?
« Reply #1 on: June 30, 2020, 07:30:49 pm »
hi

you could learn about json files in this youtube channel
covid 19 app

https://www.youtube.com/watch?v=sBcS9hfCfKI&list=PLUIPhGMf8-xogHPrOc5rt5-7ALxJzQesr

thanks

TCH

  • Full Member
  • ***
  • Posts: 200
Re: How to iterate JSON arrays?
« Reply #2 on: June 30, 2020, 07:41:29 pm »
Never mind, the crash was caused by a different part of the code, the iterating was actually working.

 

TinyPortal © 2005-2018