Sample code for 30+ languages & platforms
C

Call a JavaScript Function Returning an Array

See more JavaScript Examples

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

Chilkat C Downloads

C
#include <C_CkStringBuilder.h>
#include <C_CkJs.h>
#include <C_CkJsonObject.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkStringBuilder sbScript;
    HCkJs js;
    HCkJsonObject result;
    HCkJsonObject funcCall;
    int count;
    int i;

    success = FALSE;

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

    sbScript = CkStringBuilder_Create();
    success = CkStringBuilder_LoadFile(sbScript,"js_function_returning_array.js","utf-8");
    if (success == FALSE) {
        printf("%s\n",CkStringBuilder_lastErrorText(sbScript));
        CkStringBuilder_Dispose(sbScript);
        return;
    }

    js = CkJs_Create();

    result = CkJsonObject_Create();
    CkJsonObject_putEmitCompact(result,FALSE);

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

        // Also examine the LastErrorText.
        printf("%s\n",CkJs_lastErrorText(js));
        CkStringBuilder_Dispose(sbScript);
        CkJs_Dispose(js);
        CkJsonObject_Dispose(result);
        return;
    }

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

    funcCall = CkJsonObject_Create();

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

    CkJsonObject_UpdateString(funcCall,"name","getDays");

    success = CkJs_CallFunction(js,funcCall,result);
    if (success == FALSE) {
        // Examine the result for an exception.
        printf("%s\n",CkJsonObject_emit(result));

        // Also examine the LastErrorText.
        printf("%s\n",CkJs_lastErrorText(js));
        CkStringBuilder_Dispose(sbScript);
        CkJs_Dispose(js);
        CkJsonObject_Dispose(result);
        CkJsonObject_Dispose(funcCall);
        return;
    }

    printf("%s\n",CkJsonObject_emit(result));

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

    // Access each array value..
    count = CkJsonObject_SizeOfArray(result,"value");
    i = 0;
    while (i < count) {
        CkJsonObject_putI(result,i);
        printf("%s\n",CkJsonObject_stringOf(result,"value[i]"));
        i = i + 1;
    }

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

    CkJsonObject_Clear(funcCall);
    CkJsonObject_UpdateString(funcCall,"name","getRange");
    CkJsonObject_UpdateInt(funcCall,"args[0]",14);
    CkJsonObject_UpdateInt(funcCall,"args[1]",21);
    success = CkJs_CallFunction(js,funcCall,result);
    printf("%s\n",CkJsonObject_emit(result));

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

    count = CkJsonObject_SizeOfArray(result,"value");
    i = 0;
    while (i < count) {
        CkJsonObject_putI(result,i);
        printf("%d\n",CkJsonObject_IntOf(result,"value[i]"));
        i = i + 1;
    }

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

    CkJsonObject_Clear(funcCall);
    CkJsonObject_UpdateString(funcCall,"name","getEmployees");
    success = CkJs_CallFunction(js,funcCall,result);
    printf("%s\n",CkJsonObject_emit(result));

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

    count = CkJsonObject_SizeOfArray(result,"value");
    i = 0;
    while (i < count) {
        CkJsonObject_putI(result,i);
        printf("name: %s\n",CkJsonObject_stringOf(result,"value[i].name"));
        printf("role: %s\n",CkJsonObject_stringOf(result,"value[i].role"));
        printf("id: %d\n",CkJsonObject_IntOf(result,"value[i].id"));
        printf("\n");
        i = i + 1;
    }



    CkStringBuilder_Dispose(sbScript);
    CkJs_Dispose(js);
    CkJsonObject_Dispose(result);
    CkJsonObject_Dispose(funcCall);

    }