Sample code for 30+ languages & platforms
Java Requires Chilkat v11.4.0+

Call a JavaScript Function Returning an Object

See more JavaScript Examples

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

Chilkat Java Downloads

Chilkat2 Java
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) {
        // This is the JavaScript function we'll call:

        // function getSettings() {
        //     return {
        //         theme: "dark",
        //         notifications: true,
        //         version: 1.0
        //     };
        // }

        StringBuilder sbScript = new StringBuilder();
        sbScript.append("function getSettings() {");
        sbScript.append("    return {");
        sbScript.append("        theme: \"dark\",");
        sbScript.append("        notifications: true,");
        sbScript.append("        version: 1.0");
        sbScript.append("    };");
        sbScript.append("}");

        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 the function getSettings()

        JsonObject funcCall = new JsonObject();

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

        // {
        //   "name": "getSettings",
        // }

        funcCall.updateString("name", "getSettings");

        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": "object",
        //   "value": {
        //     "theme": "dark",
        //     "notifications": true,
        //     "version": 1
        //   }
        // }

        // Examine the object's members
        System.out.println("theme: " + result.stringOf("value.theme"));
        System.out.println("notifications: " + result.boolOf("value.notifications"));
        System.out.println("version: " + result.intOf("value.version"));

        // Output:
        // theme: dark
        // notifications: True
        // version: 1
    }
}