Sample code for 30+ languages & platforms
Lianja

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 Lianja Downloads

Lianja
llSuccess = .F.

loJson = createobject("CkJsonObject")
loJson.EmitCompact = .F.

// Assume the file contains the data as shown above..
llSuccess = loJson.LoadFile("qa_data/json/sample2.json")
if (llSuccess = .F.) then
    ? loJson.LastErrorText
    release loJson
    return
endif

// Get the "sampleData" object:
loSampleData = createobject("CkJsonObject")
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
llSuccess = loSampleData.SetNullAt(0)
// Set "answer" to null
llSuccess = loSampleData.SetNullOf("answer")

// Show the changes:
? loJson.Emit()

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

// Show the changes:
? loJson.Emit()

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

// Add a boolean value just after "pi"
llSuccess = loSampleData.AddBoolAt(1,"afterPi",.F.)

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


release loJson
release loSampleData