th3_cr0wl3r
th3_cr0wl3r

Reputation: 113

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);

Upvotes: 3

Views: 6342

Answers (2)

Ben
Ben

Reputation: 271

Try using ScriptObjectMirror:

public void call(ScriptObjectMirror obj) {
    System.out.println(obj.get("Hello"));
}

See this article for more examples: http://winterbe.com/posts/2014/04/05/java8-nashorn-tutorial/

Upvotes: 5

th3_cr0wl3r
th3_cr0wl3r

Reputation: 113

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
}

Upvotes: 1

Related Questions