1

I am using Rhino library to execute JavaScript functions in android. I have a javascript function like,

var exucuteJS = function(controlValues) {
var valueSelected = controlValues['country'];
valueSelected = valueSelected.toUpperCase();
 switch (valueSelected) {
    case "INDIA":
        return "IND_HOME";
    case "NEPAL":
        return "NEP_HOME";
    default:
        return "DEF_HOME"
 }
}

I am passing a Java HashMap object as parameter, say controlValues to the function executeJS. The problem is , the Rhino cant javascript code, to get the value from the key.

var valueSelected = controlValues['country'];

the return value is undefined.

it works fine with this line,

var valueSelected = controlValues.get('country');

but its not valid javascript code.

The same javascript is to be executed both in android and iOS. the above line will not supported in iOS. Please suggest. I am using latest version of Rhino.

1
  • i didnt undestand why downvoted. Requirement will be different for different persons. Down voting without understanding the same is very disgusting Commented Apr 7, 2016 at 9:08

3 Answers 3

2

Finally Myself found the answer.

Rather than sending a HashMap to the javascript function, need to send it as a JSONObject. Not like a normal JSONObject's object but as a Object which is in the form of NativeJSON object.

Object nativeJsonObject= NativeJSON.parse(rhino,scope,controlValueJsonString,new NullCallable());

where, rhino - Rhino's Context object. scope - Scriptable's object controlValueJsonString - JSON string equivalent to the HashMap. (controlValueJsonString = new Gson().toJson(hashMap);)

NullCallable is a class implemented from Callable,(package from rhino's org.mozilla.javascript.Callable)

this 'nativeJsonObject' should be passed to javascript function.

Object[] params = new Object[] { controlValues };

from a JSON object, javascript can get the values as,

var valueSelected = controlValues['country'];

this is working for me. Really i dont know why down voted to this question. Admins, please note this issue.

Sign up to request clarification or add additional context in comments.

1 Comment

This is not actually answering the question you asked. This answers how to convert a HashMap to a NativeObject in Java using Rhino. You already answered the question in your question. The way to access a HashMap in a javascript function using Rhino is controlValues.get('country');
0

The only way to access data of a complex oject is using its interface. You need to create separate solutions for Android and ios.

The typeof operator should help you to switch between these cases.

3 Comments

My requirement is to run the above mentioned javascript in Android. get() method is purely java. Why its not executing hashmap['key'] method to get value from hashmap
because a map is not a js object. The values are not saved in Fields.
is there any way to achieve this. I have a java HashMap, needs to be sent to javascript funcion.
0

Simpler solution where you do not have to implement a NullCallable class is

new JsonParser(context, scope).parseValue("{\"this is\":\"JSON\"}");

which I found at Rhino: return JSON from within Java

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.