AlwaysALearner
AlwaysALearner

Reputation: 6690

Javascript array to Java array using JSON Adventure

I have a situation. I'm creating a normal JavaScript array that contains a number of arrays. Since JavaScript is used completely, request.setAttribute() can't be used. So I' doing JSON.stringify(<array>) and passing the String as a parameter to a URL. This is how I'm populating my normal JavaScript array-

var arrayOfArrays = new Array();

for(var i=0;i<agent.length;i++){    
        var arrayOfStrings = new Array();
        arrayOfStrings[0] = agent[i].comp;
        arrayOfStrings[1] = agent[i].ip;
        arrayOfStrings[2] = agent[i].port;
        arrayOfStrings[3] = agent[i].username;
        arrayOfStrings[4] = agent[i].password;      

            arrayOfArrays[i] = arrayOfStrings;
}
var agents = JSON.stringify(arrayOfArrays);
// and I'm passing the agents variable as a parameter to a URL.

In the second page, I'm getting the Stringified value using request.getParameter() and trying to convert it to a Java array using the classes and methods of net.sf.json package and continuing with my logic.

The following is the array of arrays I've created which is of the form [[...],[...],[...], . . ]-

"[[\"hari2\", \"2.2.2.2\", \"222\", \"2gsz3dg\", \"sdfg2sd3\"], [\"fhf\", \"1.4.5.6\", \"678\", \"gjh\", \"gfhjgf\"], [\"hari1\", \"1.1.1.1\", \"123\", \"sdfg\", \"w34tr5\"], [\"ch\", \"1.1.1.1\", \"123\", \"ghf\", \"dgjyt\"], [\"hari\", \"1.2.3.4\", \"324\", \"xcfhd\", \"serteg34g\"], [\"hari5\", \"5.5.5.5\", \"555\", \"555\", \"555\"], [\"hari4\", \"4.4.4.4\", \"444\", \"444\", \"444\"], [\"hari3\", \"3.3.3.3\", \"333\", \"sfse\", \"3rw\"], [\"hari6\", \"6.6.6.6\", \"666\", \"666\", \"666\"]]"

Here is my logic for converting from String to array-

String agents = request.getParameter("agents");

System.out.println("Agents before removing \" :: " + agents);
if (agents.startsWith("\"") && agents.endsWith("\"")) {
    String agentsTemp = agents.replaceFirst("\"", "");
    agents = agentsTemp.substring(0, agentsTemp.length()-1);
    System.out.println("Agents after removing \" :: " + agents);
}
// applied the above logic as i had got "Invalid JSON String" Exception

if(agents != null && agents.length() > 2) {
    net.sf.json.JSONArray arrayOfArrays = (net.sf.json.JSONArray) net.sf.json.JSONSerializer.toJSON(agents); 

    for(int i=0; i<arrayOfArrays.size();i++) {
        net.sf.json.JSONArray arrayOfStrings = (net.sf.json.JSONArray) arrayOfArrays.get(i);

        String pgData = arrayOfStrings.getString(0);
        String user = arrayOfStrings.getString(3);
        String pwd = arrayOfStrings.getString(4);
        int portInt = Integer.parseInt(arrayOfStrings.getString(2));
        String hostIP = arrayOfStrings.getString(1);

                    // business logic continued
    }
} else {
    // throw exception
}


net.sf.json.JSONException: Missing value. at character 2 of [[\"hari2\", \"2.2.2.2\", \"222\", \"2gsz3dg\", \"sdfg2sd3\"], [\"fhf\", \"1.4.5.6\", \"678\", \"gjh\", \"gfhjgf\"], [\"hari1\", \"1.1.1.1\", \"123\", \"sdfg\", \"w34tr5\"], [\"ch\", \"1.1.1.1\", \"123\", \"ghf\", \"dgjyt\"], [\"hari\", \"1.2.3.4\", \"324\", \"xcfhd\", \"serteg34g\"], [\"hari5\", \"5.5.5.5\", \"555\", \"555\", \"555\"], [\"hari4\", \"4.4.4.4\", \"444\", \"444\", \"444\"], [\"hari3\", \"3.3.3.3\", \"333\", \"sfse\", \"3rw\"], [\"hari6\", \"6.6.6.6\", \"666\", \"666\", \"666\"]]

What is it expecting there? If there is any easier way to solve this issue, I'm open to suggestions.

Upvotes: 0

Views: 855

Answers (1)

Cerbrus
Cerbrus

Reputation: 72967

Just use JSON.parse().

This code:

JSON.parse("[[\"hari2\", \"2.2.2.2\", \"222\", \"2gsz3dg\", \"sdfg2sd3\"], [\"fhf\", \"1.4.5.6\", \"678\", \"gjh\", \"gfhjgf\"], [\"hari1\", \"1.1.1.1\", \"123\", \"sdfg\", \"w34tr5\"], [\"ch\", \"1.1.1.1\", \"123\", \"ghf\", \"dgjyt\"], [\"hari\", \"1.2.3.4\", \"324\", \"xcfhd\", \"serteg34g\"], [\"hari5\", \"5.5.5.5\", \"555\", \"555\", \"555\"], [\"hari4\", \"4.4.4.4\", \"444\", \"444\", \"444\"], [\"hari3\", \"3.3.3.3\", \"333\", \"sfse\", \"3rw\"], [\"hari6\", \"6.6.6.6\", \"666\", \"666\", \"666\"]]");

Outputs the correct array of arrays.:

[
    ["hari2", "2.2.2.2", "222", "2gsz3dg", "sdfg2sd3"],
    ["fhf",   "1.4.5.6", "678", "gjh",     "gfhjgf"],
    ["hari1", "1.1.1.1", "123", "sdfg",    "w34tr5"],
    ["ch",    "1.1.1.1", "123", "ghf",     "dgjyt"],
    ["hari",  "1.2.3.4", "324", "xcfhd",   "serteg34g"],
    ["hari5", "5.5.5.5", "555", "555",     "555"],
    ["hari4", "4.4.4.4", "444", "444",     "444"],
    ["hari3", "3.3.3.3", "333", "sfse",    "3rw"],
    ["hari6", "6.6.6.6", "666", "666",     "666"]
]

Upvotes: 1

Related Questions