Search code examples
javajavascriptjsonnashorn

Get Nashorn JsonObject in Java


In my JavaScript I have a JSON Object which I use as a parameter of a Java object. On the Java side I receive a jdk.nashorn.internal.scripts.JO4 but only the jdk.nashorn.internal.scripts.JO class exits. How can I access this JSON Object?

var test = {
    "id": 10,
    "Hello": "World",
    "test": {
        "Lorem" : "Ipsum",
        "java"  : true
    }
}

m.call(test);

Solution

  • With some google searches i found a solution: On the Java side, I must use a java.util.Map. So I can parse this Map to an org.json.simple.JSONObject

    public void call(Map map){
        JSONObject json = new JSONObject(map); // convert map to an json Object
    }