PureBasic
PureBasic
Call a JavaScript Function Returning an Array
See more JavaScript Examples
Demonstrates how to call a JavaScript function that returns an array.Chilkat PureBasic Downloads
IncludeFile "CkJs.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkJsonObject.pb"
Procedure ChilkatExample()
success.i = 0
; ----------------------------------------------------------------------------------
; The Javascript functions called in this example are shown at the bottom of this page.
; -----------------------------------------------------------------------------------
sbScript.i = CkStringBuilder::ckCreate()
If sbScript.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkStringBuilder::ckLoadFile(sbScript,"js_function_returning_array.js","utf-8")
If success = 0
Debug CkStringBuilder::ckLastErrorText(sbScript)
CkStringBuilder::ckDispose(sbScript)
ProcedureReturn
EndIf
js.i = CkJs::ckCreate()
If js.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
result.i = CkJsonObject::ckCreate()
If result.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::setCkEmitCompact(result, 0)
; Call Eval to add the function to the context's global object
success = CkJs::ckEval(js,sbScript,result)
If success = 0
; Examine the result for an exception.
Debug CkJsonObject::ckEmit(result)
; Also examine the LastErrorText.
Debug CkJs::ckLastErrorText(js)
CkStringBuilder::ckDispose(sbScript)
CkJs::ckDispose(js)
CkJsonObject::ckDispose(result)
ProcedureReturn
EndIf
; ------------------------------------------------------------------------------
; Call each function
funcCall.i = CkJsonObject::ckCreate()
If funcCall.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; Create JSON specifying the function name and arguments
; The function has no arguments, so we only specify the name.
CkJsonObject::ckUpdateString(funcCall,"name","getDays")
success = CkJs::ckCallFunction(js,funcCall,result)
If success = 0
; Examine the result for an exception.
Debug CkJsonObject::ckEmit(result)
; Also examine the LastErrorText.
Debug CkJs::ckLastErrorText(js)
CkStringBuilder::ckDispose(sbScript)
CkJs::ckDispose(js)
CkJsonObject::ckDispose(result)
CkJsonObject::ckDispose(funcCall)
ProcedureReturn
EndIf
Debug CkJsonObject::ckEmit(result)
; Output:
; {
; "type": "array",
; "value": [
; "Monday",
; "Tuesday",
; "Wednesday",
; "Thursday",
; "Friday"
; ]
; }
; Access each array value..
count.i = CkJsonObject::ckSizeOfArray(result,"value")
i.i = 0
While i < count
CkJsonObject::setCkI(result, i)
Debug CkJsonObject::ckStringOf(result,"value[i]")
i = i + 1
Wend
; ------------------------------------------------------------------------------
; Call the getRange(start,end) function
CkJsonObject::ckClear(funcCall)
CkJsonObject::ckUpdateString(funcCall,"name","getRange")
CkJsonObject::ckUpdateInt(funcCall,"args[0]",14)
CkJsonObject::ckUpdateInt(funcCall,"args[1]",21)
success = CkJs::ckCallFunction(js,funcCall,result)
Debug CkJsonObject::ckEmit(result)
; Output:
; {
; "type": "array",
; "value": [
; 14,
; 15,
; 16,
; 17,
; 18,
; 19,
; 20,
; 21
; ]
; }
count = CkJsonObject::ckSizeOfArray(result,"value")
i = 0
While i < count
CkJsonObject::setCkI(result, i)
Debug Str(CkJsonObject::ckIntOf(result,"value[i]"))
i = i + 1
Wend
; ------------------------------------------------------------------------------
; Call the getEmployees() function
CkJsonObject::ckClear(funcCall)
CkJsonObject::ckUpdateString(funcCall,"name","getEmployees")
success = CkJs::ckCallFunction(js,funcCall,result)
Debug CkJsonObject::ckEmit(result)
; Output:
; {
; "type": "array",
; "value": [
; {
; "id": 101,
; "name": "Alice",
; "role": "Dev"
; },
; {
; "id": 102,
; "name": "Bob",
; "role": "Manager"
; }
; ]
; }
count = CkJsonObject::ckSizeOfArray(result,"value")
i = 0
While i < count
CkJsonObject::setCkI(result, i)
Debug "name: " + CkJsonObject::ckStringOf(result,"value[i].name")
Debug "role: " + CkJsonObject::ckStringOf(result,"value[i].role")
Debug "id: " + Str(CkJsonObject::ckIntOf(result,"value[i].id"))
Debug ""
i = i + 1
Wend
CkStringBuilder::ckDispose(sbScript)
CkJs::ckDispose(js)
CkJsonObject::ckDispose(result)
CkJsonObject::ckDispose(funcCall)
ProcedureReturn
EndProcedure