PowerBuilder
PowerBuilder
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 PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_Json
string ls_JsonStr
oleobject loo_AbcObj
oleobject loo_XArray
oleobject loo_BObj
oleobject loo_DocRoot
li_Success = 0
loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")
if li_rc < 0 then
destroy loo_Json
MessageBox("Error","Connecting to COM object failed")
return
end if
ls_JsonStr = "{~"flower~": ~"tulip~",~"abc~":{~"x~": [{ ~"a~" : 1 },{ ~"b1~" : 100, ~"b2~" : 200 },{ ~"c~" : 3 }],~"y~": 200,~"z~": 200}}"
li_Success = loo_Json.Load(ls_JsonStr)
if li_Success = 0 then
Write-Debug loo_Json.LastErrorText
destroy loo_Json
return
end if
// Get the "abc" object.
loo_AbcObj = create oleobject
li_rc = loo_AbcObj.ConnectToNewObject("Chilkat.JsonObject")
li_Success = loo_Json.ObjectOf2("abc",loo_AbcObj)
if li_Success = 0 then
Write-Debug loo_Json.LastErrorText
destroy loo_Json
destroy loo_AbcObj
return
end if
// Side note: The JSON of a sub-part of the document can be emitted from any JSON object:
loo_AbcObj.EmitCompact = 0
Write-Debug loo_AbcObj.Emit()
// Navigate to the "x" array
loo_XArray = create oleobject
li_rc = loo_XArray.ConnectToNewObject("Chilkat.JsonArray")
loo_AbcObj.ArrayOf2("x",loo_XArray)
// Navigate to the 2nd object contained within the array. This contains members b1 and b2
loo_BObj = create oleobject
li_rc = loo_BObj.ConnectToNewObject("Chilkat.JsonObject")
loo_XArray.ObjectAt2(1,loo_BObj)
// Show that we're at "b1/b2".
// The value of "b1" should be "200"
Write-Debug "b2 = " + string(loo_BObj.IntOf("b2"))
// Now go back to the JSON doc root:
loo_DocRoot = create oleobject
li_rc = loo_DocRoot.ConnectToNewObject("Chilkat.JsonObject")
loo_BObj.GetDocRoot2(loo_DocRoot)
// 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.
loo_DocRoot.EmitCompact = 0
Write-Debug loo_DocRoot.Emit()
destroy loo_Json
destroy loo_AbcObj
destroy loo_XArray
destroy loo_BObj
destroy loo_DocRoot