AutoIt
AutoIt
Get the Root of a JSON Document
See more JSON Examples
Demonstrates how to get back to the JSON root object from anywhere in the JSON document. This example uses the following JSON document:
{
"flower": "tulip",
"abc":
{
"x": [
{ "a" : 1 },
{ "b1" : 100, "b2" : 200 },
{ "c" : 3 }
],
"y": 200,
"z": 200
}
}
Chilkat AutoIt Downloads
Local $bSuccess = False
$oJson = ObjCreate("Chilkat.JsonObject")
Local $sJsonStr = "{""flower"": ""tulip"",""abc"":{""x"": [{ ""a"" : 1 },{ ""b1"" : 100, ""b2"" : 200 },{ ""c"" : 3 }],""y"": 200,""z"": 200}}"
$bSuccess = $oJson.Load($sJsonStr)
If ($bSuccess = False) Then
ConsoleWrite($oJson.LastErrorText & @CRLF)
Exit
EndIf
; Get the "abc" object.
$oAbcObj = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oJson.ObjectOf2("abc",$oAbcObj)
If ($bSuccess = False) Then
ConsoleWrite($oJson.LastErrorText & @CRLF)
Exit
EndIf
; Side note: The JSON of a sub-part of the document can be emitted from any JSON object:
$oAbcObj.EmitCompact = False
ConsoleWrite($oAbcObj.Emit() & @CRLF)
; Navigate to the "x" array
$oXArray = ObjCreate("Chilkat.JsonArray")
$oAbcObj.ArrayOf2("x",$oXArray)
; Navigate to the 2nd object contained within the array. This contains members b1 and b2
$oBObj = ObjCreate("Chilkat.JsonObject")
$oXArray.ObjectAt2(1,$oBObj)
; Show that we're at "b1/b2".
; The value of "b1" should be "200"
ConsoleWrite("b2 = " & $oBObj.IntOf("b2") & @CRLF)
; Now go back to the JSON doc root:
$oDocRoot = ObjCreate("Chilkat.JsonObject")
$oBObj.GetDocRoot2($oDocRoot)
; We'll skip the null check and assume it's non-null...
; Pretty-print the JSON doc from the root to show that this is indeed the root.
$oDocRoot.EmitCompact = False
ConsoleWrite($oDocRoot.Emit() & @CRLF)