如何将 Java Map 转换为基本的 Javascript 对象?
我开始使用 Java 6 中的动态 rhinoscript 功能,供那些更了解 Javascript 而不是 Java 的客户使用。
将 Map(关联数组、javascript obj 等)传递到 Javascript 以便脚本编写者可以使用标准 Javascript 点表示法来访问值的最佳方法是什么?
我当前正在将 java.util.Map 值传递到脚本中,但是脚本编写者必须编写“map.get('mykey')”而不是“map.mykey”。
基本上,我想做与这个问题相反的事情。
I'm starting to use the dynamic rhinoscript feature in Java 6 for use by customers who are more likely to know Javascript than Java.
What is the best way to pass a Map (associative array, javascript obj, whatever) into Javascript so the script-writers can use the standard Javascript dot notation for accessing values?
I'm currently passing a java.util.Map of values into the script, however then the script writer has to write "map.get('mykey')" instead of "map.mykey".
Basically, I want to do the opposite of this question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我采用了 Java NativeObject 方法,这就是我所做的......
I took the Java NativeObject approach and here is what I did...
我正在使用一个实用程序类,将 Map 转换为 javascript 哈希对象:
示例:
I am using an utility class that convert Map into javascript hash object:
Sample:
在弄清楚
SimpleScriptContext
将仅采用Map
对象并从而强制您在 JavaScript 中使用 Java 方法之后,这就是我所做的。哪个打印出来了
After figuring out that the
SimpleScriptContext
will only take theMap
object and thus force you to use the Java methods in your JavaScript, here's what I did.Which printed out
您只需将对象编码为 JSON,手动或使用 杰克逊 或 gson。正如您所说,这与该问题完全相反,并且该问题的作者对 JSON 表示法不满意:)
您需要发送到浏览器的内容基本上是这样的:
然后 javascript 开发人员可以简单地访问: <代码>someObject.key2。
You just need to encode your object as JSON, either manually, or using a library like Jackson or gson. As you said, it's the exact oposite of that question and the author of that question is not happy with the JSON notation :)
What you need to send to the browser is basically something like this:
And then the javascript developer can simply access:
someObject.key2
.