0

I need to pass this below array [aLocations] from jsp.

<script type="text/javascript">
  var aLocations = new Array();
  var aTitles = new Array();
  var aDetails = new Array();

  aLocations = ['keswick,cumbria,uk','grasmere,cumbria,uk','ambleside,cumbria,uk'];

</script>

if i tried like this, it is not working:

<%
ArrayList<String> myArr = new ArrayList<String>();
        myArr.add("keswick,cumbria,uk");
        myArr.add("grasmere,cumbria,uk");
        myArr.add("ambleside,cumbria,uk");
%>

<script type="text/javascript">
  var aLocations = new Array();
  var aTitles = new Array();
  var aDetails = new Array();

  aLocations = <%=myArr%>;

</script>

what is the best approach to pass from JSP?

1 Answer 1

0

You need a library. For example with http://code.google.com/p/google-gson/ you can:

Gson gson = new Gson();
aLocations = <%=gson.toJson(myArr)%>;

or see https://sites.google.com/site/gson/gson-user-guide for documentation.

Without a library you need to manually create a structure like ["imtem1","item2",..,"itemn"] with iteration, string concatenation, escape handling, etc. See Populating JavaScript Array from JSP List

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

2 Comments

thanks for ur quick response. cant be achievable without that library?
i tried as u mentioned but i am getting this error: "The method fromObject(ArrayList<String>) is undefined for the type JSONArray"

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.