gpt4 book ai didi

使用 JSON Adventure 将 Javascript 数组转换为 Java 数组

转载 作者:行者123 更新时间:2023-12-01 14:51:22 25 4
gpt4 key购买 nike

我有一个情况。我正在创建一个包含多个数组的普通 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.

在第二页中,我使用 request.getParameter() 获取字符串化值并尝试将其转换为 Java array使用 net.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\"]]

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

最佳答案

只需使用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"]
]

关于使用 JSON Adventure 将 Javascript 数组转换为 Java 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14809712/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com