How to get values from a Java HashMap in javascript function using Rhino

AndEngine

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.

AndEngine

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get the values from the Hashmap without using Iterator?

From Dev

How to get the values from the Hashmap without using Iterator?

From Dev

How to get a specific duplicate values keys using hashMap in java

From Dev

Invoke JavaScript callback from a Java method asynchronously using Rhino

From Dev

How can I get the version of Rhino from within javascript?

From Dev

How can I get the version of Rhino from within javascript?

From Dev

Rhino pass Java class to Javascript function

From Dev

Rhino pass Java class to Javascript function

From Dev

How to get 5 highest values from a hashmap?

From Dev

How to sum values from Java Hashmap

From Dev

Java builder pattern accessible from JavaScript in Rhino

From Dev

How to get function parameter values from an HTML Page using bookmarklet?

From Dev

How to get key position from a HashMap in Java

From Dev

How can I use java 8 streams to get values from my hashmap

From Dev

How can I use java 8 streams to get values from my hashmap

From Dev

How To Get All Values In HashMap Using globalMap.get()

From Dev

How can I read values from XML and save it in Hashmap using Java

From Dev

How to get values from posted html form using javascript

From Dev

How to get values from a form posted using javascript

From Dev

How to get values from associative array using Javascript

From Dev

Get values from HashMap as reference

From Dev

Get values from HashMap as reference

From Dev

How to get the minOccurs / maxOccurs values from the XSD in Java using JAXB?

From Dev

How to get values from matrix using arraylist JAVA

From Dev

How to get multiple values for multiple keys from Hashmap in a single operation?

From Dev

How to get particular hashmap keys and values from json objects

From Dev

How to get element from Java's LinkedList in constant time using HashMap

From Dev

Get one of the values from Hashmap with multiple values

From Dev

How to make Rhino to run custom javascript setTimeout function, endless eval

Related Related

  1. 1

    How to get the values from the Hashmap without using Iterator?

  2. 2

    How to get the values from the Hashmap without using Iterator?

  3. 3

    How to get a specific duplicate values keys using hashMap in java

  4. 4

    Invoke JavaScript callback from a Java method asynchronously using Rhino

  5. 5

    How can I get the version of Rhino from within javascript?

  6. 6

    How can I get the version of Rhino from within javascript?

  7. 7

    Rhino pass Java class to Javascript function

  8. 8

    Rhino pass Java class to Javascript function

  9. 9

    How to get 5 highest values from a hashmap?

  10. 10

    How to sum values from Java Hashmap

  11. 11

    Java builder pattern accessible from JavaScript in Rhino

  12. 12

    How to get function parameter values from an HTML Page using bookmarklet?

  13. 13

    How to get key position from a HashMap in Java

  14. 14

    How can I use java 8 streams to get values from my hashmap

  15. 15

    How can I use java 8 streams to get values from my hashmap

  16. 16

    How To Get All Values In HashMap Using globalMap.get()

  17. 17

    How can I read values from XML and save it in Hashmap using Java

  18. 18

    How to get values from posted html form using javascript

  19. 19

    How to get values from a form posted using javascript

  20. 20

    How to get values from associative array using Javascript

  21. 21

    Get values from HashMap as reference

  22. 22

    Get values from HashMap as reference

  23. 23

    How to get the minOccurs / maxOccurs values from the XSD in Java using JAXB?

  24. 24

    How to get values from matrix using arraylist JAVA

  25. 25

    How to get multiple values for multiple keys from Hashmap in a single operation?

  26. 26

    How to get particular hashmap keys and values from json objects

  27. 27

    How to get element from Java's LinkedList in constant time using HashMap

  28. 28

    Get one of the values from Hashmap with multiple values

  29. 29

    How to make Rhino to run custom javascript setTimeout function, endless eval

HotTag

Archive