6

I have a Java Map. I want to convert it in to JavaScript map.

The java function to convert as JS map follows:

private Object getJSLocalizedValueMap() {

    Map<String, String> langSel = new HashMap<String, String>();
    langSel.add("en", true);
    langSel.add("de", false);
    langSel.add("fr", false);

    //Now convert this map into Javascript Map
    NativeObject nobj = new NativeObject();
    ScriptEngineManager factory = new ScriptEngineManager();
    ScriptEngine engine = factory.getEngineByName("javascript");
    for (Map.Entry<String, String> entry : langSel.entrySet()) {
        nobj.defineProperty(entry.getKey(), entry.getValue(), NativeObject.READONLY);
    }
    engine.put("langSel", nobj);
    return langSel;
}

In the JSP page's javascript the code is:

var langs = ${messagesJS};

In Javascript, I got:

langs = {en=true, de=false, fr=false};

instead of

langs = {"en":true, "de":false, "fr":false}

Please suggest me how to achieve this?

1
  • 1
    Is there any reason you need to have the keys wrapped in double quotes? You already have a valid JavaScript object. If it's an absolute requirement (and, again, I can't think of a single reason why it would be), take a look at returning JSON. Commented Dec 12, 2012 at 13:23

2 Answers 2

5

You might be able to use the JSONObject class for this. It has a constructor that you pass in a Map. Call that and then call the toString() method and it should give you a JS array.

http://www.json.org/javadoc/org/json/JSONObject.html#JSONObject%28java.util.Map%29

So something like:

Map<String, String> langSel = new HashMap<String, String>();
langSel.add("en", true);
langSel.add("de", false);
langSel.add("fr", false);

JSONObject jsonObj = new JSONObject(langSel);
engine.put("langSel", jsonObj.toString());

You will need to make sure the org.json.JSONObject class is on your classpath. Either download the JAR and add to classpath manually (through Eclipse or something) or if you are using Maven use a dependency like the following:

  <dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20070829</version>
  </dependency>

If you are not using Maven, you can still download the JAR from here: http://mvnrepository.com/artifact/org.json/json/20090211

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

4 Comments

How to get the JSONObject class. It is giving class path not found.
Thanks cowls, I am able to add in the class path. But, still I am getting the string as {en=true, de=false, fr=false} (JSP map) instead of {"en":true, "de":false, "fr":false} (javaScript map) in JSP page.
I would need to see the code. Make sure you are not still returning langSel, as this is a java Map and will give that output, you should be returning the json object
Thanks cowls, Its working fine. Acutually, I created a JSONObject by new JSONObject() function and then added each map entry to jsonObj by converting the second string to Boolean. Now, It is working fine. Thank you.
2

Instead of below line

langSel.add("en", true);

use this line

langSel.add("\"en\"", true);

Try this it might helpful.

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.