是否有将字符串转换为数组的最佳方法。
我正在使用 Spring mvc 并从 javascript 调用 ajax 并获取字符串
var result = "[[a,b,c],[d,e,f]]"
我想像这样将这个字符串转换为数组
var resultArray = [[a,b,c],[d,e,f]]
当我找到长度时,我想得到 2
最佳答案
之前我返回了一个列表列表,例如
List<List<String>> list = new ArrayList<List<String>>();
因此,在 JSON 解析期间,jQuery 函数出现错误,因为我得到的是“[[a,b,c],[d,e,f]]
”,而不是 double引号(“a”)。然后我更改了后端代码,它返回 JsonArray 的 JsonArray,如 [["a","b","c"],["d","e","f"]]
,现在我可以解析 JSON。
JSONArray outerJsonArray = new JSONArray();
JSONArray innerJsonArray = new JSONArray();
innerJsonArray.put("a");
innerJsonArray.put("b");
innerJsonArray.put("c");
outerJsonArray.put(innerJsonArray);
innerJsonArray.put("d");
innerJsonArray.put("e");
innerJsonArray.put("f");
outerJsonArray.put(innerJsonArray);
并返回outerJsonArray.toString()
关于javascript - 如何将数组字符串转换为数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35055815/