I am trying to process the data from a NativeObject
, but I have found a small issue with the naming of the object properties. The JavaScript code for the object is:
var mainObject = {
"innerObject" : {
"234" : {
"property1" : "1",
"property2" : "2"
}
}
}
The method I use to process it in Java looks like this:
public void processNative(NativeObject obj) {
if(NativeObject.hasProperty(obj, "innerObject")) {
NativeObject no = (NativeObject)NativeObject.getProperty(obj, "innerObject");
Object[] propIds = NativeObject.getPropertyIds(no);
for (int i = 0; i < propIds.length; i++) {
String key = propIds[i].toString();
NativeObject numObj = (NativeObject)NativeObject.getProperty(no, key);
//do more processing here
}
}
}
This code will throw this exception: java.lang.ClassCastException: org.mozilla.javascript.UniqueTag cannot be cast to org.mozilla.javascript.NativeObject
.
If I cast to a UniqueTag
I can actually get the proper value, which in this case will be UniqueTag.NOT_FOUND
.
I find this a bit confusing since I am getting the property key from the object itself and it actually exists in the JavaScript code.
If I change the name of the object from "234" to "car" the code works as expected, so I am assuming that there is an issue with the naming. From this post I understand that property names can be any type of string, so there should be no problems with having a string made of digits.
So I guess a follow-up question would be: Is there a way for me to solve this issue without having to do the renaming?
One more thing I need to mention is that I only have access to the JavaScript code and to the Java method. Everything else is a black box.
Thanks!