RepublicCommando

Looping through a json file hierarchically

2 posts in this topic

#1 ·  Posted (edited)

Hi, I am trying to use This Json UDF to load a json file and display it in a _GUICtrlTreeView.

The code is quite large, but I have separated it into GUI script, functionality script, file functions script. The first two working perfectly so far.

Two functions in my file functions script are responsible for the loading of a json file and telling my functionality script to do something with it:

Func _ltf_loadfile($fPath)
   Local $Object = Json_ObjCreate() ;Declare object properly.
   $Object = Json_Decode(FileRead($fPath)) ;Decode file into this object
   If Json_IsObject($Object) Then ;If the object is valid, we can use it.
      objLoop($Object) ;Handle information
   Else ;Object invalid? Tell user we can't load the file!
      MsgBox(0, "Error", "Json possibly has wrong syntax," & @CRLF & "Cannot open file.")
   EndIf
EndFunc

Func objLoop($hObj)
   Local $aItems = Json_ObjGetKeys($hObj) ;Get the keys of this object
   For $row=0 To UBound($aItems,1)-1
      ;MsgBox(0, "Rows", "Rows " & $row+1 & " of " & UBound($aItems, 1))
      $nObj = Json_ObjGet($hObj, $aItems[$row])
      If Json_IsObject($nObj) Then
         For $column=0 to UBound($aItems,2)-1
            _lt_AddChildItem($aItems[$row][$column])
            MsgBox(0,"",$aItems[$row][$column]) ;Will display a Message Box with that Array's Cell content
         Next
      EndIf
      _lt_AddChildItem($aItems[$row])
   Next
      ;_GUICtrlTreeView_SelectItem ($hLangTree, $nObj)
      ;_lt_AddChildItem($aItems[$i]) ;Function in another script that adds a GUI TreeView Item, with optional name.
EndFunc

but I am having problems iterating past the first column.

If need be I can post all my code, but I know it all works up until this point because of thorough testing.

Here is an example JSON I've been using for testing:

{
"Game":{
   "Name":"Game Title Goes Here",
   "Interface":{
      "MainMenu":{
         "bOpts":"Options",
         "bMp":"MultiPlayer",
         "bSp":"SinglePlayer",
         "bCreds":"Credits",
         "bQuit":"Quit Game"
      }
   }
   "Input":{
      "inMouse":"Mouse",
      "inJoy":"Joy Stick",
      "inKeys":"KeyBoard"
   }
}
"key2":"Key2 Text",
"key3":"Key3 Text",
"key4":"Key4 Text",
"num":1024,
"digits":{"1","2","3","4"}
}

I can stick _lt_AddChildItem() in the row for-loop and get items all day long, but I can't seem to get child objects or items from the first row.

Doing so results in "Game|key2|key3|key4|num|digits" in vertical order on my _GUITreeView, just no child items or objects for "Game" object.

Thank you for reading!

edit- Corrected some syntax with the last item in an object having commas when the shouldn't.

Edited by RepublicCommando

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I think I managed to solve your problem. Sorry reviving this dead post, but I saw this  and it looked like he was having the same problem when I was searching for a solution to mine.

4 minutes ago, uncommon said:

Figured it out. Use the key name in the object to get the next level object, if you do not know what will be in the next object and you need to iterate use the Keys function like this $Object.keys to return an array of the nested keys. When you get the final Parent object of the key you want to remove I used the builtin Json_ObjDelete function to remove the target key and value.

The Sample code below removes the bOpts Key and value. Hopefully this helps someone in the future.

Sample Code

$sJsonString = FileRead(@DesktopDir & "\codebeautify.json")
    $oJsonString = Json_Decode($sJsonString)
    $oItems = $oJsonString("Game")
    $oParts = $oItems("Interface")
    $oMenu = $oParts("MainMenu")
    $aMenu = $oMenu.keys
    _ArrayDisplay($aMenu)
    Json_ObjDelete($oMenu, "bOpts")

Sample Json

{
"Game":{
   "Name":"Game Title Goes Here",
   "Interface":{
      "MainMenu":{
         "bOpts":"Options",
         "bMp":"MultiPlayer",
         "bSp":"SinglePlayer",
         "bCreds":"Credits",
         "bQuit":"Quit Game"
      }
   }
   "Input":{
      "inMouse":"Mouse",
      "inJoy":"Joy Stick",
      "inKeys":"KeyBoard"
   }
}
"key2":"Key2 Text",
"key3":"Key3 Text",
"key4":"Key4 Text",
"num":1024,
"digits":{"1","2","3","4"}
}

 

 

Edited by uncommon

No problem can withstand the assault of sustained thinking.Voltaire

_Array2HTMLTable()_IEClassNameGetCollection()_IEquerySelectorAll()

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now