0

我有一个情况。我正在创建一个包含许多数组的普通 JavaScript 数组。由于JavaScript完全使用,request.setAttribute()不能使用。所以我做JSON.stringify(<array>)并将字符串作为参数传递给 URL。这就是我填充普通 JavaScript 数组的方式-

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.

在第二页中,我使用包的类和方法获取 Stringified 值request.getParameter()并尝试将其转换为 Java ,并继续我的逻辑。arraynet.sf.json

以下是我创建的数组数组,其形式为[[...],[...],[...], . . ]-

"[[\"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\"]]"

这是我从字符串转换为数组的逻辑-

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\"]]

它在那里期待什么?如果有任何更简单的方法来解决这个问题,我愿意接受建议。

4

1 回答 1

1

只需使用JSON.parse().

这段代码:

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\"]]");

输出正确的数组数组。:

[
    ["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"]
]
于 2013-02-11T10:21:39.220 回答