Sample code for 30+ languages & platforms
Unicode C++

Call a JavaScript Function Returning an Array

See more JavaScript Examples

Demonstrates how to call a JavaScript function that returns an array.

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkStringBuilderW.h>
#include <CkJsW.h>
#include <CkJsonObjectW.h>

void ChilkatSample(void)
    {
    bool success = false;

    //  ----------------------------------------------------------------------------------
    //  The Javascript functions called in this example are shown at the bottom of this page.
    //  -----------------------------------------------------------------------------------

    CkStringBuilderW sbScript;
    success = sbScript.LoadFile(L"js_function_returning_array.js",L"utf-8");
    if (success == false) {
        wprintf(L"%s\n",sbScript.lastErrorText());
        return;
    }

    CkJsW js;

    CkJsonObjectW result;
    result.put_EmitCompact(false);

    //  Call Eval to add the function to the context's global object
    success = js.Eval(sbScript,result);
    if (success == false) {
        //  Examine the result for an exception.
        wprintf(L"%s\n",result.emit());

        //  Also examine the LastErrorText.
        wprintf(L"%s\n",js.lastErrorText());
        return;
    }

    //  ------------------------------------------------------------------------------
    //  Call each function

    CkJsonObjectW funcCall;

    //  Create JSON specifying the function name and arguments
    //  The function has no arguments, so we only specify the name.

    funcCall.UpdateString(L"name",L"getDays");

    success = js.CallFunction(funcCall,result);
    if (success == false) {
        //  Examine the result for an exception.
        wprintf(L"%s\n",result.emit());

        //  Also examine the LastErrorText.
        wprintf(L"%s\n",js.lastErrorText());
        return;
    }

    wprintf(L"%s\n",result.emit());

    //  Output:
    //  {
    //    "type": "array",
    //    "value": [
    //      "Monday",
    //      "Tuesday",
    //      "Wednesday",
    //      "Thursday",
    //      "Friday"
    //    ]
    //  }

    //  Access each array value..
    int count = result.SizeOfArray(L"value");
    int i = 0;
    while (i < count) {
        result.put_I(i);
        wprintf(L"%s\n",result.stringOf(L"value[i]"));
        i = i + 1;
    }

    //  ------------------------------------------------------------------------------
    //  Call the getRange(start,end) function

    funcCall.Clear();
    funcCall.UpdateString(L"name",L"getRange");
    funcCall.UpdateInt(L"args[0]",14);
    funcCall.UpdateInt(L"args[1]",21);
    success = js.CallFunction(funcCall,result);
    wprintf(L"%s\n",result.emit());

    //  Output:
    //  {
    //    "type": "array",
    //    "value": [
    //      14,
    //      15,
    //      16,
    //      17,
    //      18,
    //      19,
    //      20,
    //      21
    //    ]
    //  }

    count = result.SizeOfArray(L"value");
    i = 0;
    while (i < count) {
        result.put_I(i);
        wprintf(L"%d\n",result.IntOf(L"value[i]"));
        i = i + 1;
    }

    //  ------------------------------------------------------------------------------
    //  Call the getEmployees() function

    funcCall.Clear();
    funcCall.UpdateString(L"name",L"getEmployees");
    success = js.CallFunction(funcCall,result);
    wprintf(L"%s\n",result.emit());

    //  Output:
    //  {
    //    "type": "array",
    //    "value": [
    //      {
    //        "id": 101,
    //        "name": "Alice",
    //        "role": "Dev"
    //      },
    //      {
    //        "id": 102,
    //        "name": "Bob",
    //        "role": "Manager"
    //      }
    //    ]
    //  }

    count = result.SizeOfArray(L"value");
    i = 0;
    while (i < count) {
        result.put_I(i);
        wprintf(L"name: %s\n",result.stringOf(L"value[i].name"));
        wprintf(L"role: %s\n",result.stringOf(L"value[i].role"));
        wprintf(L"id: %d\n",result.IntOf(L"value[i].id"));
        wprintf(L"\n");
        i = i + 1;
    }
    }