Recent

Author Topic: Parsing array with FPJson  (Read 6176 times)

ELCouz

  • New Member
  • *
  • Posts: 11
Parsing array with FPJson
« on: December 09, 2021, 03:27:11 am »
Hi,

I am scratching a bit my head over why parsing this JSON array doesn't work at all.

Source JSON:

Code: Text  [Select][+][-]
  1. [
  2.     {
  3.         "address": 63,
  4.         "connected": true,
  5.         "stateKnown": true,
  6.         "pairingEnabled": false,
  7.         "lifterShuttleLightBarrierBreached": false,
  8.         "warningSignalOn": false,
  9.         "emergencyStopTriggered": false,
  10.         "openDoors": {
  11.             "mainDoor": false,
  12.             "markedSecondaryDoors": [1,2,3,4]
  13.         },
  14.         "requestedAccess": {
  15.             "mainDoor": false,
  16.             "markedSecondaryDoors": []
  17.         },
  18.         "isTestModeActive": false,
  19.         "schematicId": "",
  20.         "notifictionsByType": {}
  21.     },
  22.  
  23. (...)
  24.  
  25. ]
  26.  

Specific code always returning nil. I am trying to get the variable array at "markedSecondaryDoors".

Code: Pascal  [Select][+][-]
  1.          
  2.           BackDoors := TJSONArray(Json.FindPath('openDoors.markedSecondaryDoors'));
  3.           If BackDoors <> nil then
  4.           begin
  5.             for a := 0 to BackDoors.Count - 1 do
  6.             begin
  7.               BackDoorInt := BackDoors.Items[a].AsInteger;
  8.  
  9.               Case BackDoorInt of
  10.               1:QRY.ParamByName('BackDoor1').AsBoolean := True;
  11.               2:QRY.ParamByName('BackDoor2').AsBoolean := True;
  12.               3:QRY.ParamByName('BackDoor3').AsBoolean := True;
  13.               4:QRY.ParamByName('BackDoor4').AsBoolean := True;
  14.               end;
  15.             end;
  16.           end;

The json variable (TJSONData) have all the json stored inside which I use to catch the rest of variable without any issue with Json.Items[].FindPath('') function.

Any clue why TJSONArray doesn't catch it ?

Thanks!

Laurent
« Last Edit: December 09, 2021, 03:30:45 am by ELCouz »

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Parsing array with FPJson
« Reply #1 on: December 09, 2021, 06:12:57 am »
Code: Pascal  [Select][+][-]
  1. {$H+}
  2.  
  3. uses
  4.     fpjson,jsonparser;
  5.  
  6. const
  7.     JSONStr =
  8.         '[' + LineEnding +
  9.         '    {' + LineEnding +
  10.         '        "address": 63,' + LineEnding +
  11.         '        "connected": true,' + LineEnding +
  12.         '        "stateKnown": true,' + LineEnding +
  13.         '        "pairingEnabled": false,' + LineEnding +
  14.         '        "lifterShuttleLightBarrierBreached": false,' + LineEnding +
  15.         '        "warningSignalOn": false,' + LineEnding +
  16.         '        "emergencyStopTriggered": false,' + LineEnding +
  17.         '        "openDoors": {' + LineEnding +
  18.         '            "mainDoor": false,' + LineEnding +
  19.         '            "markedSecondaryDoors": [1,2,3,4]' + LineEnding +
  20.         '        },' + LineEnding +
  21.         '        "requestedAccess": {' + LineEnding +
  22.         '            "mainDoor": false,' + LineEnding +
  23.         '            "markedSecondaryDoors": []' + LineEnding +
  24.         '        },' + LineEnding +
  25.         '        "isTestModeActive": false,' + LineEnding +
  26.         '        "schematicId": "",' + LineEnding +
  27.         '        "notifictionsByType": {}' + LineEnding +
  28.         '    }' + LineEnding +
  29.         ']'
  30.         ;
  31.  
  32. var
  33.     LJSONArr,LSecondaryDoorsJSONArr: TJSONArray;
  34.     LJSONEnum: TJSONEnum;
  35. begin
  36.     LJSONArr := TJSONArray(GetJSON(JSONStr));
  37.     LSecondaryDoorsJSONArr := TJSONArray(LJSONArr.FindPath('[0].openDoors.markedSecondaryDoors'));
  38.     if Assigned(LSecondaryDoorsJSONArr) then
  39.         for LJSONEnum in LSecondaryDoorsJSONArr do
  40.             WriteLn(LJSONEnum.Value.AsInteger);
  41. end.
  42.  
  43.  

ELCouz

  • New Member
  • *
  • Posts: 11
Re: Parsing array with FPJson
« Reply #2 on: December 10, 2021, 02:26:28 am »
Oh thanks it worked!

I forgot to go one level deeper in the json structure! :-[

I am really enjoying my switch from Delphi to Lazarus, can't beat the vast platform choice! :)

Have a nice day, Leledumbo.

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1111
  • Professional amateur ;-P
Re: Parsing array with FPJson
« Reply #3 on: December 10, 2021, 06:38:55 pm »
Hey ELCouz,
I am really enjoying my switch from Delphi to Lazarus, can't beat the vast platform choice! :)

+1

No bigger truth has been spoken, :D

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

 

TinyPortal © 2005-2018