spring mvc json格式数组到后台数组的转换

前台会这样发请求:
points是个数组
$.post($.basePath+"/services/addLines.do",
{"points":'"['+points.toString()+']"'},
function(data){
alert(data);
});
后台这样接收
@RequestMapping(value = "/addLines")

public @ResponseBody String addLines(
@RequestParam(value = "points", required = false)String param) {
// /fpline/services/addLines.do
System.out.println(param.length());
System.out.println(param);
return "ok";
}
现在我能在后台接收到points的字符串形式,如:[33,22,33,22,33]
现在的问题是如何将整个字符串转换为 int[].
我想用属性编辑器做这个处理,在addLines 方法的入口处可以直接拿到int[]
最新回答
ら面具背后の殇う

2024-09-22 07:40:56

springmvc后台ajax调用中字符串到jquery中的json对象和数组对象转换问题

查看jquery文档,我们知道jquery有很多种Ajax调用方法,下面结合springmvc返回的数据,假设
的是data ='{"label":"1","value":[{"id":1,"pid":null,"name":"娱乐"},{"id":101,"pid":1,"name":"音乐"},{"id":112,"pid":1,"name":"卡通"}],"text":""}',
则在如下的ajax调用和回调中,可以参考范例:
$.get("test.do", function(data){
alert("Data Loaded: " + data);
var jsonObj = eval('(' + data + ')'); //获得jsonObj对象
alert(jsonObj.label);
alert(jsonObj.value);
var arrObj = eval(jsonObj.value);//然后就可以对arrObj做数组运算了
alert(arrObj );}

结果:
data 获取到'{"label":"1","value":[{"id":1,"pid":null,"name":"娱乐"},{"id":101,"pid":1,"name":"音乐"},{"id":112,"pid":1,"name":"卡通"}],"text":""}';
json.label获取到1;
json.value获取到[{"id":1,"pid":null,"name":"娱乐"},{"id":101,"pid":1,"name":"音乐"},{"id":112,"pid":1,"name":"卡通"}]数组;
arrObj 获取到":[{"id":1,"pid":null,"name":"娱乐"},{"id":101,"pid":1,"name":"音乐"},{"id":112,"pid":1,"name":"卡通"}]
追问
回答的好快,我写补充的过程中就来了2个,能加qq详谈吗?告我qq,我加你
玖梦

2024-09-22 05:52:45

json包么,把json数据转化为想要的格式
仅有的温存丶

2024-09-22 06:58:25

org.json.JSONObject jobj = null;

String json = request.getParameter("json");
System.out.println("json=" + json);
jobj = new org.json.JSONObject(json);

JSONArray相似做法