Java Requires Chilkat v11.4.0+
Chilkat2 Java
Call a JavaScript Function Returning an Array
See more JavaScript Examples
Demonstrates how to call a JavaScript function that returns an array.Chilkat Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Js;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.StringBuilder;
public class ChilkatExample {
public static void main(String[] args) {
// ----------------------------------------------------------------------------------
// The Javascript functions called in this example are shown at the bottom of this page.
// -----------------------------------------------------------------------------------
StringBuilder sbScript = new StringBuilder();
try {
sbScript.loadFile("js_function_returning_array.js", "utf-8");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
Js js = new Js();
JsonObject result = new JsonObject();
result.setEmitCompact(false);
// Call Eval to add the function to the context's global object
try {
js.eval(sbScript, result);
} catch (ChilkatException e) {
// Examine the result for an exception.
System.out.println(result.emit());
// Also examine the LastErrorText.
System.out.println(e.getLastErrorText());
return;
}
// ------------------------------------------------------------------------------
// Call each function
JsonObject funcCall = new JsonObject();
// Create JSON specifying the function name and arguments
// The function has no arguments, so we only specify the name.
funcCall.updateString("name", "getDays");
try {
js.callFunction(funcCall, result);
} catch (ChilkatException e) {
// Examine the result for an exception.
System.out.println(result.emit());
// Also examine the LastErrorText.
System.out.println(e.getLastErrorText());
return;
}
System.out.println(result.emit());
// Output:
// {
// "type": "array",
// "value": [
// "Monday",
// "Tuesday",
// "Wednesday",
// "Thursday",
// "Friday"
// ]
// }
// Access each array value..
int count = result.sizeOfArray("value");
int i = 0;
while (i < count) {
result.setI(i);
System.out.println(result.stringOf("value[i]"));
i = i + 1;
}
// ------------------------------------------------------------------------------
// Call the getRange(start,end) function
funcCall.clear();
funcCall.updateString("name", "getRange");
funcCall.updateInt("args[0]", 14);
funcCall.updateInt("args[1]", 21);
js.callFunction(funcCall, result);
System.out.println(result.emit());
// Output:
// {
// "type": "array",
// "value": [
// 14,
// 15,
// 16,
// 17,
// 18,
// 19,
// 20,
// 21
// ]
// }
count = result.sizeOfArray("value");
i = 0;
while (i < count) {
result.setI(i);
System.out.println(result.intOf("value[i]"));
i = i + 1;
}
// ------------------------------------------------------------------------------
// Call the getEmployees() function
funcCall.clear();
funcCall.updateString("name", "getEmployees");
js.callFunction(funcCall, result);
System.out.println(result.emit());
// Output:
// {
// "type": "array",
// "value": [
// {
// "id": 101,
// "name": "Alice",
// "role": "Dev"
// },
// {
// "id": 102,
// "name": "Bob",
// "role": "Manager"
// }
// ]
// }
count = result.sizeOfArray("value");
i = 0;
while (i < count) {
result.setI(i);
System.out.println("name: " + result.stringOf("value[i].name"));
System.out.println("role: " + result.stringOf("value[i].role"));
System.out.println("id: " + result.intOf("value[i].id"));
System.out.println("");
i = i + 1;
}
}
}
JavaScript
function getDays() {
return ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"];
}
function getRange(start, end) {
var arr = [];
for (var i = start; i <= end; i++) {
arr.push(i);
}
return arr;
}
// Usage: getRange(1, 5) -> [1, 2, 3, 4, 5]
function getEmployees() {
return [
{ id: 101, name: "Alice", role: "Dev" },
{ id: 102, name: "Bob", role: "Manager" }
];
}