在java和javascript之间传递JSON的有效方法 [英] Effective way to pass JSON between java and javascript

查看:102
本文介绍了在java和javascript之间传递JSON的有效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Nashorn的新手,并且在JVM之上编写脚本并且想知道我是否可以让我的java代码和javascripts更有效地进行通信。

I'm fairly new to Nashorn and scripting on top of the JVM and wanted to know if I can get my java code and javascripts to communicate more effectively.

我正在使用与JS对象一起使用的第三方JS库,在我的java代码中,我将要传递的数据作为 Map< String,Object>数据

I'm using a 3rd party JS lib that works with JS objects, and in my java code I have the data I want to pass as a Map<String, Object> data.

因为第三方JS希望使用普通的JS对象,所以无法传递我的数据虽然脚本引擎允许您访问Map,就好像它是一个普通的JS对象一样。

Because that 3rd party JS expects to work with plain JS objects I can't pass my data as is, although the script engine allows you to access a Map as if it was a plain JS object.

我正在使用的脚本在数据参数上使用'hasOwnProperty',并在Java对象上调用时失败。

The script i'm using uses 'hasOwnProperty' on the data argument and fails when invoked on an Java object.

当我尝试使用Object.prototype.hasOwnProperty.call(data,'myProp')时,它也无效并且总是返回'false'。基本问题是Java Object不是javascript对象原型。

When I tried using Object.prototype.hasOwnProperty.call(data, 'myProp') it also didn't work and always returned 'false'. The basic problem is that a Java Object is not a javascript object prototype.

我最终做了这样的事情:

what I ended up doing something like this :

Map<String, Object> data;

ObjectMapper mapper = new ObjectMapper();   
String rawJSON = mapper.writeValueAsString(data);

ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");

engine.eval('third_party_lib.js');
engine.eval('function doSomething(jsonStr) { var jsObj = JSON.parse(jsonStr); return doSomethingElse(jsObj); }');
Object value = ((Invocable) engine).invokeFunction("doSomething", rawJSON);

这是按预期工作的,但所有这些JSON来回解析很重,感觉可能会有更简单,更快速,更直接的方法。

This works as expected, but all this JSON parsing back and forth is heavy and feels like there might be simpler, faster and more straight forward way to do it.

那么,是否有更好的方法在Java和Javascript之间传递JSON或创建兼容的JS对象的方法在我的Java代码中?

So, is there a better way to pass JSON between Java and Javascript or a way to create a compatible JS object in my Java code ?

我见过这个使用mustache.js进行模板渲染的指南,但它做的几乎完全相同。

I've seen this guide to template rendering using mustache.js but it's doing pretty much the same thing.

谢谢!

推荐答案

Nashorn专门处理java.util.Map对象。 Nashorn允许将Map键视为属性。另见 https://wiki.openjdk.java.net/display / Nashorn / Nashorn + extensions#Nashornextensions-SpecialtreatmentofofjectsofpecificJavaclasses

Nashorn treats java.util.Map objects specially. Nashorn allows Map keys to be treated as "properties". See also https://wiki.openjdk.java.net/display/Nashorn/Nashorn+extensions#Nashornextensions-SpecialtreatmentofobjectsofspecificJavaclasses

因此,如果您的地图包含foo作为键,脚本可以访问mapObj.foo以获取它的值。您评估的脚本是第三方脚本无关紧要。只要该脚本由Nashorn评估,nashorn将专门链接Map属性访问并获得您想要的结果。这种方法避免了不必要的JSON字符串转换和解析往返(正如您自己提到的那样)。

So if your map contains "foo" as key, script can access mapObj.foo to get it's value. It does not matter the script you evaluated is third-party one. As long as the script is evaluated by Nashorn, nashorn will specially link Map property access and get the result you want. This approach avoid unnecessary JSON string conversion and parse round trip (as you yourself mentioned).

这篇关于在java和javascript之间传递JSON的有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆