Sample code for 30+ languages & platforms
Visual FoxPro

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 Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loJson
LOCAL loSampleData

lnSuccess = 0

loJson = CreateObject('Chilkat.JsonObject')
loJson.EmitCompact = 0

* Assume the file contains the data as shown above..
lnSuccess = loJson.LoadFile("qa_data/json/sample2.json")
IF (lnSuccess = 0) THEN
    ? loJson.LastErrorText
    RELEASE loJson
    CANCEL
ENDIF

* Get the "sampleData" object:
loSampleData = CreateObject('Chilkat.JsonObject')
loJson.ObjectOf2("sampleData",loSampleData)

* Demonstrate BoolAt and BoolOf
? "hungry: " + STR(loSampleData.BoolOf("hungry"))
? "hungry: " + STR(loSampleData.BoolAt(2))

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

* Demonstrate IsNullOf / IsNullAt
? "withoutValue is null? " + STR(loSampleData.IsNullOf("withoutValue"))
? "withoutValue is null? " + STR(loSampleData.IsNullAt(3))
? "apple is null? " + STR(loSampleData.IsNullOf("apple"))
? "apple is null? " + STR(loSampleData.IsNullAt(1))

* IntOf
? "answer: " + STR(loSampleData.IntOf("answer"))

* SetNullAt, SetNullOf
* Set "pi" to null
lnSuccess = loSampleData.SetNullAt(0)
* Set "answer" to null
lnSuccess = loSampleData.SetNullOf("answer")

* Show the changes:
? loJson.Emit()

* Restore pi and apple:
lnSuccess = loSampleData.SetNumberAt(0,"3.14")
lnSuccess = loSampleData.SetNumberOf("answer","42")

* Show the changes:
? loJson.Emit()

* Add a null value named "afterApple" just after "apple"
lnSuccess = loSampleData.AddNullAt(2,"afterApple")

* Add a boolean value just after "pi"
lnSuccess = loSampleData.AddBoolAt(1,"afterPi",0)

* Examine the changes..
? loJson.Emit()

RELEASE loJson
RELEASE loSampleData