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 <C_CkStringBuilderW.h>
#include <C_CkJsW.h>
#include <C_CkJsonObjectW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkStringBuilderW sbScript;
    HCkJsW js;
    HCkJsonObjectW result;
    HCkJsonObjectW funcCall;
    int count;
    int i;

    success = FALSE;

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

    sbScript = CkStringBuilderW_Create();
    success = CkStringBuilderW_LoadFile(sbScript,L"js_function_returning_array.js",L"utf-8");
    if (success == FALSE) {
        wprintf(L"%s\n",CkStringBuilderW_lastErrorText(sbScript));
        CkStringBuilderW_Dispose(sbScript);
        return;
    }

    js = CkJsW_Create();

    result = CkJsonObjectW_Create();
    CkJsonObjectW_putEmitCompact(result,FALSE);

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

        // Also examine the LastErrorText.
        wprintf(L"%s\n",CkJsW_lastErrorText(js));
        CkStringBuilderW_Dispose(sbScript);
        CkJsW_Dispose(js);
        CkJsonObjectW_Dispose(result);
        return;
    }

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

    funcCall = CkJsonObjectW_Create();

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

    CkJsonObjectW_UpdateString(funcCall,L"name",L"getDays");

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

        // Also examine the LastErrorText.
        wprintf(L"%s\n",CkJsW_lastErrorText(js));
        CkStringBuilderW_Dispose(sbScript);
        CkJsW_Dispose(js);
        CkJsonObjectW_Dispose(result);
        CkJsonObjectW_Dispose(funcCall);
        return;
    }

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

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

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

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

    CkJsonObjectW_Clear(funcCall);
    CkJsonObjectW_UpdateString(funcCall,L"name",L"getRange");
    CkJsonObjectW_UpdateInt(funcCall,L"args[0]",14);
    CkJsonObjectW_UpdateInt(funcCall,L"args[1]",21);
    success = CkJsW_CallFunction(js,funcCall,result);
    wprintf(L"%s\n",CkJsonObjectW_emit(result));

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

    count = CkJsonObjectW_SizeOfArray(result,L"value");
    i = 0;
    while (i < count) {
        CkJsonObjectW_putI(result,i);
        wprintf(L"%d\n",CkJsonObjectW_IntOf(result,L"value[i]"));
        i = i + 1;
    }

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

    CkJsonObjectW_Clear(funcCall);
    CkJsonObjectW_UpdateString(funcCall,L"name",L"getEmployees");
    success = CkJsW_CallFunction(js,funcCall,result);
    wprintf(L"%s\n",CkJsonObjectW_emit(result));

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

    count = CkJsonObjectW_SizeOfArray(result,L"value");
    i = 0;
    while (i < count) {
        CkJsonObjectW_putI(result,i);
        wprintf(L"name: %s\n",CkJsonObjectW_stringOf(result,L"value[i].name"));
        wprintf(L"role: %s\n",CkJsonObjectW_stringOf(result,L"value[i].role"));
        wprintf(L"id: %d\n",CkJsonObjectW_IntOf(result,L"value[i].id"));
        wprintf(L"\n");
        i = i + 1;
    }



    CkStringBuilderW_Dispose(sbScript);
    CkJsW_Dispose(js);
    CkJsonObjectW_Dispose(result);
    CkJsonObjectW_Dispose(funcCall);

    }