Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Java List to Javascript Array

I have the following java code in my Android application and wanted a way to convert the Java list to an array that can be used in javascript:

Java:

public void onCompleted(List<GraphUser> users, Response response) {
    for(int i = 0; i < users.size(); i++)
    {
             //add to an array object that can be used in Javascript
             webView.loadUrl("javascript:fetchFriends(arrObj)");        
    }               
 }

Javascript:

  //this is how I want to be able to use the object in Javascript
    function parseFriends(usersObjectFromJava){
       var users = [];
        for (var i = 0; i < usersObjectFromJava.length; i++) {
            var u = {
                Id: usersObjectFromJava[i].id + "",
                UserName: usersObjectFromJava[i].username,
                FirstName: usersObjectFromJava[i].first_name,
                LastName: usersObjectFromJava[i].last_name,
            };
            users[i] = u;
        }
    }

Could some help me with the Java code to create the usersObjectFromJava so that it can be used in javascript?

like image 945
Prabhu Avatar asked Jan 10 '14 16:01

Prabhu


3 Answers

Use GSON to convert java objects to JSON string, you can do it by

Gson gson = new Gson();
TestObject o1 = new TestObject("value1", 1);
TestObject o2 = new TestObject("value2", 2);
TestObject o3 = new TestObject("value3", 3);

List<TestObject> list = new ArrayList<TestObject>();
list.add(o1);
list.add(o2);
list.add(o3);

gson.toJson(list) will give you

[{"prop1":"value1","prop2":2},{"prop1":"value2","prop2":2},{"prop1":"value3","prop2":3}]

Now you can use JSON.parse(), to deserialize from JSON to Javascript Object.

like image 184
NarendraSoni Avatar answered Oct 22 '22 21:10

NarendraSoni


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

like image 7
bbuecherl Avatar answered Oct 22 '22 23:10

bbuecherl


You can use Gson Library.

Gson gson = new GsonBuilder().create();
JsonArray jsonArray = gson.toJsonTree(your_list, TypeClass.class).getAsJsonArray();

http://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/Gson.html

like image 1
xild Avatar answered Oct 22 '22 23:10

xild