Sample code for 30+ languages & platforms
Xbase++ Requires Chilkat v11.0.0+

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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oJson
LOCAL oSampleData

nSuccess := 0

oJson := CreateObject("Chilkat.JsonObject")
oJson:EmitCompact := 0

//  Assume the file contains the data as shown above..
nSuccess := oJson:LoadFile("qa_data/json/sample2.json")
IF (nSuccess == 0)
    ? oJson:LastErrorText
    oJson:destroy()
    RETURN
ENDIF

//  Get the "sampleData" object:
oSampleData := CreateObject("Chilkat.JsonObject")
oJson:ObjectOf2("sampleData", oSampleData)

//  Demonstrate BoolAt and BoolOf
? "hungry: " + Str(oSampleData:BoolOf("hungry"))
? "hungry: " + Str(oSampleData:BoolAt(2))

//  StringOf returns the value as a string regardless of it's actual type:
? "pi: " + oSampleData:StringOf("pi")
? "answer: " + oSampleData:StringOf("answer")
? "withoutValue: " + oSampleData:StringOf("withoutValue")
? "hungry: " + oSampleData:StringOf("hungry")

//  Demonstrate IsNullOf / IsNullAt
? "withoutValue is null? " + Str(oSampleData:IsNullOf("withoutValue"))
? "withoutValue is null? " + Str(oSampleData:IsNullAt(3))
? "apple is null? " + Str(oSampleData:IsNullOf("apple"))
? "apple is null? " + Str(oSampleData:IsNullAt(1))

//  IntOf
? "answer: " + Str(oSampleData:IntOf("answer"))

//  SetNullAt, SetNullOf
//  Set "pi" to null
nSuccess := oSampleData:SetNullAt(0)
//  Set "answer" to null
nSuccess := oSampleData:SetNullOf("answer")

//  Show the changes:
? oJson:Emit()

//  Restore pi and apple:
nSuccess := oSampleData:SetNumberAt(0, "3.14")
nSuccess := oSampleData:SetNumberOf("answer", "42")

//  Show the changes:
? oJson:Emit()

//  Add a null value named "afterApple" just after "apple"
nSuccess := oSampleData:AddNullAt(2, "afterApple")

//  Add a boolean value just after "pi"
nSuccess := oSampleData:AddBoolAt(1, "afterPi", 0)

//  Examine the changes..
? oJson:Emit()

oJson:destroy()
oSampleData:destroy()