Converting a Java Arraylist of Strings to a JavaScript Array

Converting a Java ArrayList of strings to a JavaScript array

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

Convert ArrayListString to String[] array

Use like this.

List<String> stockList = new ArrayList<String>();
stockList.add("stock1");
stockList.add("stock2");

String[] stockArr = new String[stockList.size()];
stockArr = stockList.toArray(stockArr);

for(String s : stockArr)
System.out.println(s);

How to convert arraylist to string in Javascript?

You can use the Array.join() with an empty string as the separator

var digitStack = [];digitStack.push(1);digitStack.push(2);digitStack.push(3);var string = digitStack.join('');
snippet.log(string)
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 --><script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

Convert Java List to Javascript Array

I would assume doing this:

Java:

public void onCompleted(List<GraphUser> users, Response response) {
JSONArray arr = new JSONArray();
JSONObject tmp;
try {
for(int i = 0; i < users.size(); i++) {
tmp = new JSONObject();
tmp.put("Id",users.get(i).id); //some public getters inside GraphUser?
tmp.put("Username",users.get(i).username);
tmp.put("FirstName",users.get(i).first_name);
tmp.put("LastName",users.get(i).last_name);
arr.add(tmp);
}

webView.loadUrl("javascript:fetchFriends("+arr.toString()+")");
} catch(JSONException e){
//error handling
}
}

JavaScript:

function fetchFriends(usersObjectFromJava){
var users = usersObjectFromJava;
}

You will have to change the Java-Code a bit (i.e. using public getters or add more/less information to the JSONObjects.
JSON is included in Android by default, so no external libraries are necessary.

I hope i understood your problem.

Small thing i came across: you where using fetchFriends in Java but its called parseFriends in Javascript, I renamed them to fetchFriends

How do I pass Java array contents into javascript array?

If you want to use Java code to write the code for a Javascript data structure, probably the simplest way to do it is to use a JSON library for Java. A JSON string can be interpreted as Javascript code.

If you want to use JSON.simple, then there are examples here of how to generate the JSON string:

http://code.google.com/p/json-simple/wiki/EncodingExamples

In your code, you should be able to do something like this:

var instanceIds = <%= JSONValue.toJSONString(serverIds) %>

You shouldn't need to convert your ArrayList to a Java array. Note that this function is sensitive to what type you pass into it; an array actually won't work in this instance.

Also, to do this you will need to install the JSON.simple JAR file and import org.json.simple.JSONValue in your JSP.



Related Topics



Leave a reply



Submit