Rhino - 将 javascript 对象传递给 java [英] Rhino - Passing a javascript object to java
问题描述
我对 Rhino 很陌生,我的问题是如何实现以下目标,
Im quite new to Rhino and my question is around how to achieve the following,
假设我有一个 javascript 对象,它遵循以下类似的内容,我可以在 java 中使用它.
Say I have a javascript object that follows something like the following that I can consume within java.
var myObject = new Object();
myObject.string1 = "Hello";
myObject.string2 = "World";
myObject.int1 = 1;
但是如果它是动态的,我如何在 java 中使用它.例如如果您决定在 javascript 中向此对象动态添加更多成员.这是可行的吗?我的猜测是在 java 中定义的类必须考虑所有可能的成员才能做到这一点?
But how do I consume this within java if its dynamic. For .e.g. if you decide to add few more members dynamically to this object within javascript. Is this doable ? My guess is the class defined within java will have to take all the possible members into account to do this ?
希望我已经解释了我试图正确实现的目标.
Hopefully I have explained what im trying to achieve correctly.
推荐答案
JavaScript 对象,当您在 Java 中访问它们时,本质上都是同一个类:ScriptableObject
,它实现了 Scriptable
接口 (GitHub 源代码).还有一些其他类用于函数和其他专用对象.
JavaScript objects, when you access them in Java, are all essentially the same class: ScriptableObject
which implements the Scriptable
interface (GitHub source). There are a few other classes for functions and other specialized objects.
Scriptable
接口包括 get
、has
和 put
等方法,大致对应于 JavaScript 中的 myObject.string1
、myObject.hasOwnProperty("string1")
和 myObject.string1 = "Hello"
.ScriptableObject
类添加了一些其他有用的方法来定义属性等.
The Scriptable
interface includes methods like get
, has
, and put
that correspond roughly to myObject.string1
, myObject.hasOwnProperty("string1")
, and myObject.string1 = "Hello"
in JavaScript. The ScriptableObject
class adds some other useful methods for defining properties, etc.
这篇关于Rhino - 将 javascript 对象传递给 java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!