Recent

Author Topic: [SOLVED] another json problem  (Read 620 times)

superc

  • Full Member
  • ***
  • Posts: 241
[SOLVED] another json problem
« on: March 29, 2023, 11:04:31 am »
Hello,

I've another json like this
Code: Pascal  [Select][+][-]
  1. {
  2.         "ProductCode": "aabbcc",
  3.         "type": {
  4.                 "Input": [{
  5.                         "A1": {
  6.                                 "code": 1,
  7.                                 "LinkDevice": [
  8.                                         1, 2, 3
  9.  
  10.                                 ],
  11.                                 "PosX": 100,
  12.                                 "PosY": 100
  13.                         },
  14.                         "A2": {
  15.                                 "code": 2,
  16.                                 "LinkDevice": [
  17.                                         4, 5, 6, 7, 8
  18.                                 ],
  19.                                 "PosX": 100,
  20.                                 "PosY": 120
  21.                         },
  22.                         "A3": {
  23.                                 "code": 3,
  24.                                 "LinkDevice": [
  25.                                         2, 3, 5
  26.                                 ],
  27.                                 "PosX": 100,
  28.                                 "PosY": 140
  29.                         }
  30.                 }],
  31.                 "Version": "1"
  32.         }
  33. }
  34.  

I must retrieve all data from all element of "Input", like "A1", "A2", "A3" surely they can change and therefore I have to cycle them with an index, but I don't understand how to do it,
 thanks in advance.


« Last Edit: March 31, 2023, 08:11:09 am by superc »

paweld

  • Hero Member
  • *****
  • Posts: 988
Re: another json problem
« Reply #1 on: March 29, 2023, 12:18:47 pm »
Code: Pascal  [Select][+][-]
  1. uses
  2.   fpjson;
  3.  
  4. procedure TForm1.Button1Click(Sender: TObject);
  5. var
  6.   jd: TJSONData;
  7.   ja: TJSONArray;
  8.   jo: TJSONObject;
  9.   i, j, k: Integer;
  10. begin
  11.   Memo2.Lines.Clear;
  12.   jd := GetJSON(Memo1.Lines.Text);
  13.   if (jd.FindPath('type.Input') <> nil) and (jd.GetPath('type.Input').Count > 0) then
  14.   begin
  15.     ja := TJSONArray(jd.GetPath('type.Input'));
  16.     for i := 0 to ja.Count - 1 do
  17.     begin
  18.       for j := 0 to ja.Objects[i].Count - 1 do
  19.       begin
  20.         Memo2.Lines.Add(ja.Objects[i].Names[j]);
  21.         jo := TJSONObject(ja.Objects[i].GetPath(ja.Objects[i].Names[j]));
  22.         for k := 0 to jo.Count - 1 do
  23.         begin
  24.           if jo.GetPath(jo.Names[k]).JSONType = jtArray then
  25.             Memo2.Lines.Add(Format('    %s is ARRAY', [jo.Names[k]]))
  26.           else if jo.GetPath(jo.Names[k]).JSONType = jtObject then
  27.             Memo2.Lines.Add(Format('    %s id OBJECT', [jo.Names[k]]))
  28.           else
  29.             Memo2.Lines.Add(Format('    %s > %s', [jo.Names[k], jo.GetPath(jo.Names[k]).AsString]));
  30.         end;
  31.       end;
  32.     end;
  33.   end;
  34.   jd.Free;
  35. end;  
Best regards / Pozdrawiam
paweld

superc

  • Full Member
  • ***
  • Posts: 241
Re: another json problem
« Reply #2 on: March 29, 2023, 12:33:14 pm »
it's perfect but i didn't consider one thing: i need to fetch the data of LinkDevice and i would like to massively fetch the array without having to loop it, as if it were a string, is it possible?

Code: Pascal  [Select][+][-]
  1. begin
  2.   tl := TStringList.Create;
  3.   tl.LoadFromFile(fileJson);
  4.  
  5.   jd := GetJSON(tl.Text);
  6.   if (jd.FindPath('type.Input') <> nil) and (jd.GetPath('type.Input').Count > 0) then
  7.   begin
  8.     ja := TJSONArray(jd.GetPath('type.Input'));
  9.     for i := 0 to ja.Count - 1 do
  10.     begin
  11.       for j := 0 to ja.Objects[i].Count - 1 do
  12.       begin
  13.         memoDebug.Lines.Add(ja.Objects[i].Names[j]);
  14.         jo := TJSONObject(ja.Objects[i].GetPath(ja.Objects[i].Names[j]));
  15.         for k := 0 to jo.Count - 1 do
  16.         begin
  17.           if jo.GetPath(jo.Names[k]).JSONType = jtArray then
  18.           begin
  19.             jaa := TJSONArray(jo.GetPath(jo.Names[k]));
  20. //            memoDebug.Lines.Add(Format('    %s is ARRAY', [jo.Names[k]]))
  21.               for h := 0 to jaa.Count-1 do
  22.               begin
  23.                 // concatenate
  24.                //inc(cnt);
  25.               end;
  26.           end
  27.           else if jo.GetPath(jo.Names[k]).JSONType = jtObject then
  28.             memoDebug.Lines.Add(Format('    %s id OBJECT', [jo.Names[k]]))
  29.           else
  30.             memoDebug.Lines.Add(Format('    %s > %s', [jo.Names[k], jo.GetPath(jo.Names[k]).AsString]));
  31.         end;
  32.       end;
  33.     end;
  34.   end;
  35.   jd.Free;
  36. end;    

I modified code like this, but I must concatenate... Thanks in advance.
« Last Edit: March 29, 2023, 12:40:44 pm by superc »

 

TinyPortal © 2005-2018