Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
like image 343
th3_cr0wl3r Avatar asked Aug 13 '26 02:08

th3_cr0wl3r


2 Answers

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/

like image 58
Ben Avatar answered Aug 14 '26 16:08

Ben


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
}
like image 32
th3_cr0wl3r Avatar answered Aug 14 '26 16:08

th3_cr0wl3r



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!