The following data:
sku=[111222]
is being passed in as an argument to a javascript function being invoked in the Rhino javascript engine.
On the java side (where the data originates) this means that a String key of "sku" is mapped to an ArrayList with one String "111222" inside it.
Once this goes over to the Rhino side, I have no idea how to make use of this as a javascript object. For example: How do I even do something as simple as fetching the 111222 value on the JS side?
I decided to spit out what a key/value dump of this object looked like on the JS side and this is what I got:
sku=
empty: false
indexOf: function indexOf() {/*
int indexOf(java.lang.Object)
*/}
notifyAll: function notifyAll() {/*
void notifyAll()
*/}
removeAll: function removeAll() {/*
boolean removeAll(java.util.Collection)
*/}
trimToSize: function trimToSize() {/*
void trimToSize()
*/}
containsAll: function containsAll() {/*
boolean containsAll(java.util.Collection)
*/}
contains: function contains() {/*
boolean contains(java.lang.Object)
*/}
equals: function equals() {/*
boolean equals(java.lang.Object)
*/}
notify: function notify() {/*
void notify()
*/}
subList: function subList() {/*
java.util.List subList(int,int)
*/}
class: class java.util.ArrayList
set: function set() {/*
java.lang.Object set(int,java.lang.Object)
*/}
isEmpty: function isEmpty() {/*
boolean isEmpty()
*/}
add: function add() {/*
void add(int,java.lang.Object)
boolean add(java.lang.Object)
*/}
ensureCapacity: function ensureCapacity() {/*
void ensureCapacity(int)
*/}
size: function size() {/*
int size()
*/}
iterator: function iterator() {/*
java.util.Iterator iterator()
*/}
clear: function clear() {/*
void clear()
*/}
wait: function wait() {/*
void wait()
void wait(long)
void wait(long,int)
*/}
listIterator: function listIterator() {/*
java.util.ListIterator listIterator(int)
java.util.ListIterator listIterator()
*/}
retainAll: function retainAll() {/*
boolean retainAll(java.util.Collection)
*/}
toString: function toString() {/*
java.lang.Strin开发者_StackOverflowg toString()
*/}
hashCode: function hashCode() {/*
int hashCode()
*/}
toArray: function toArray() {/*
java.lang.Object[] toArray(java.lang.Object[])
java.lang.Object[] toArray()
*/}
lastIndexOf: function lastIndexOf() {/*
int lastIndexOf(java.lang.Object)
*/}
addAll: function addAll() {/*
boolean addAll(java.util.Collection)
boolean addAll(int,java.util.Collection)
*/}
clone: function clone() {/*
java.lang.Object clone()
*/}
get: function get() {/*
java.lang.Object get(int)
*/}
getClass: function getClass() {/*
java.lang.Class getClass()
*/}
remove: function remove() {/*
java.lang.Object remove(int)
boolean remove(java.lang.Object)
*/}
Can anyone tell me how to work with such objects in rhino javascript engine?
The JavaScript program is receiving a Java ArrayList; this is why the above dump matches the API of java.util.ArrayList
. (See the Java API documentation.) You can call the Java methods of that Java object relatively transparently from your JavaScript code. So, for example, to loop through the elements of the array you are receiving:
var sku = [however you are getting it];
for (var i=0; i<sku.size(); i++) {
var nextElement = sku.get(i);
// do something
}
Caveat: Assuming the ArrayList is created on the Java side and is creating Java objects, the algorithm above will put a Java java.lang.String
into the nextElement
variable. This object may behave strangely if your application is JavaScript-centric (for example, typeof(nextElement) == "object" /* not "string" */
). If you want a JavaScript string, you'll need to convert it; the easiest way is String(nextElement)
.
精彩评论