Converting a Java ArrayList of strings to a JavaScript array
Asked Answered
A

6

12

In my Java code, I have an ArrayList of Strings. I'd like to put this data in a JavaScript variable on a JSP page I'm working on. My first thought was to include it directly, e.g.:

var myArray = <%= arrayList %>;

Unfortunately, when executed, myArray is a string in the format [a,b,c], not an actual JavaScript array. How do I get some data from a Java ArrayList to a JavaScript array?

Alvin answered 3/7, 2013 at 5:14 Comment(1)
please add more code with an example dataBorosilicate
P
5

When you use <%=arraylist%> it calls the toString() on list and prints [a,b,c]

And No,you cannot direclty convert From Java arrayList to javascript array ,Convert the Java ArrayList to JSON String, and use JSON.parse() to get Javascript object.

Have a look at Json objet and Json in java

Pyemia answered 3/7, 2013 at 5:15 Comment(3)
Thanks buddy. I have the json string like [a,b,c]. When i use Json.parse(aa); It shows the error. Uncaught reference error.a is not defined. So it must be like ["a","b","c"]. So how to convert like this?Alvin
Dude,you have to add json library to your class path and convert your arraylist on serverside to json string and send it to client.Then JSON.parse() works :)Pyemia
The second link in the answer is not available anymore.Kylstra
R
13

Withoug a library:

Java:

public static String toJavascriptArray(String[] arr){
    StringBuffer sb = new StringBuffer();
    sb.append("[");
    for(int i=0; i<arr.length; i++){
        sb.append("\"").append(arr[i]).append("\"");
        if(i+1 < arr.length){
            sb.append(",");
        }
    }
    sb.append("]");
    return sb.toString();
}

JSP:

var myArray = <%= toJavascriptArray(arrayList) %>;
Rambo answered 3/7, 2013 at 6:1 Comment(1)
Technique is good, but still you need to parse it to JSON, while assigning to javascript variable.Credulous
P
5

When you use <%=arraylist%> it calls the toString() on list and prints [a,b,c]

And No,you cannot direclty convert From Java arrayList to javascript array ,Convert the Java ArrayList to JSON String, and use JSON.parse() to get Javascript object.

Have a look at Json objet and Json in java

Pyemia answered 3/7, 2013 at 5:15 Comment(3)
Thanks buddy. I have the json string like [a,b,c]. When i use Json.parse(aa); It shows the error. Uncaught reference error.a is not defined. So it must be like ["a","b","c"]. So how to convert like this?Alvin
Dude,you have to add json library to your class path and convert your arraylist on serverside to json string and send it to client.Then JSON.parse() works :)Pyemia
The second link in the answer is not available anymore.Kylstra
D
2

Do the following in your JSP page

<% List<String> strList = new ArrayList<String>();
strList.add("one");
strList.add("two");
strList.add("three"); %>

var jsArray = [<% for (int i = 0; i < strList.size(); i++) { %>"<%= strList.get(i) %>"<%= i + 1 < strList.size() ? ",":"" %><% } %>];

The output will be

var jsArray = ["one","two","three"];

If your List was empty it will output

var jsArray = [];
Dagenham answered 20/8, 2013 at 23:12 Comment(0)
O
2

The JavaScript split() method returns an array, so it's a simple way to convert a Java ArrayList into a JavaScript array.

function toJavascript(){
    var array="<%=javaArrayList%>";
    array=array.replace("[", "");
    array=array.replace("]", "");
    return javaArray.split(",");
}
Oribelle answered 14/9, 2014 at 12:27 Comment(0)
W
1

try this way:

var myArray = <%=net.sf.json.JSONSerializer.toJSON(arrayList) %>;
Whiteley answered 3/7, 2013 at 5:59 Comment(0)
S
0

Simple

 var jsArray = JSON.parse(yourJavaListObject)
Scampi answered 15/3, 2023 at 9:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.