PowerBuilder
PowerBuilder
JSON: Miscellaneous Operations
See more JSON Examples
Demonstrates a variety of JSON API methods. This example uses the following JSON document:
{
"alphabet": "abcdefghijklmnopqrstuvwxyz",
"sampleData" : {
"pi": 3.14,
"apple": "juicy",
"hungry": true,
"withoutValue": null,
"answer": 42
}
}
Chilkat PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_Json
oleobject loo_SampleData
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
loo_Json.EmitCompact = 0
// Assume the file contains the data as shown above..
li_Success = loo_Json.LoadFile("qa_data/json/sample2.json")
if li_Success = 0 then
Write-Debug loo_Json.LastErrorText
destroy loo_Json
return
end if
// Get the "sampleData" object:
loo_SampleData = create oleobject
li_rc = loo_SampleData.ConnectToNewObject("Chilkat.JsonObject")
loo_Json.ObjectOf2("sampleData",loo_SampleData)
// Demonstrate BoolAt and BoolOf
Write-Debug "hungry: " + string(loo_SampleData.BoolOf("hungry"))
Write-Debug "hungry: " + string(loo_SampleData.BoolAt(2))
// StringOf returns the value as a string regardless of it's actual type:
Write-Debug "pi: " + loo_SampleData.StringOf("pi")
Write-Debug "answer: " + loo_SampleData.StringOf("answer")
Write-Debug "withoutValue: " + loo_SampleData.StringOf("withoutValue")
Write-Debug "hungry: " + loo_SampleData.StringOf("hungry")
// Demonstrate IsNullOf / IsNullAt
Write-Debug "withoutValue is null? " + string(loo_SampleData.IsNullOf("withoutValue"))
Write-Debug "withoutValue is null? " + string(loo_SampleData.IsNullAt(3))
Write-Debug "apple is null? " + string(loo_SampleData.IsNullOf("apple"))
Write-Debug "apple is null? " + string(loo_SampleData.IsNullAt(1))
// IntOf
Write-Debug "answer: " + string(loo_SampleData.IntOf("answer"))
// SetNullAt, SetNullOf
// Set "pi" to null
li_Success = loo_SampleData.SetNullAt(0)
// Set "answer" to null
li_Success = loo_SampleData.SetNullOf("answer")
// Show the changes:
Write-Debug loo_Json.Emit()
// Restore pi and apple:
li_Success = loo_SampleData.SetNumberAt(0,"3.14")
li_Success = loo_SampleData.SetNumberOf("answer","42")
// Show the changes:
Write-Debug loo_Json.Emit()
// Add a null value named "afterApple" just after "apple"
li_Success = loo_SampleData.AddNullAt(2,"afterApple")
// Add a boolean value just after "pi"
li_Success = loo_SampleData.AddBoolAt(1,"afterPi",0)
// Examine the changes..
Write-Debug loo_Json.Emit()
destroy loo_Json
destroy loo_SampleData