加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

AJAX向SpringMVC controller 传JSONArray并将String转换成JSONAr

发布时间:2020-12-16 00:35:14 所属栏目:百科 来源:网络整理
导读:http://stackoverflow.com/questions/14515785/how-to-convert-json-string-arrray-to-json-array-list create JSONArray ([{},{},{}]) in JS var data = [];data.push({"id" : 1,"val" : $('#projectName').val()});data.push({"id" : 2,"val" : $('#descri

http://stackoverflow.com/questions/14515785/how-to-convert-json-string-arrray-to-json-array-list


create JSONArray ([{},{},{}]) in JS

var data = [];
data.push({
		"id" : 1,"val" : $('#projectName').val()
	});
	data.push({
		"id" : 2,"val" : $('#description').val()
	});
	data.push({
		"id" : 3,"val" : $('#startdate').val()
	});
	data.push({
		"id" : 4,"val" : STATUS_NOT_START
	});
	var id = 5;
	while($("#" + id).length > 0){
		data.push({
			"id" : id,"val" : $("#" + id).val()
		});
		id++;
	}

ajax pass JSONArray String to Controller:
$.ajax({
		type : "Post",url : "createProject.html",data : "jsonArray=" + JSON.stringify(data) + "&depId=" + depId + "&groId=" + groId + "&objId=" + objId,success : function(response){
			var alertText = "Project " + $('#projectName').val() + " is successfully created! Project ID: " + response;
			addAlert("alert-success",alertText,"#alertdiv");
		},error : function(e){
			var alertText = 'Error: ' + e;
     		addAlert("alert-error","#alertdiv");
		}
	});

Controller将JSONArray String转换成JSONArray
import net.sf.json.JSONArray; JSONException; JSONObject; JSONSerializer; public class TestJson { static void parseProfilesJson(String the_json){ try { JSONArray nameArray =(JSONArray) JSONSerializer.toJSON(the_json); System.out.println(nameArray.size()); for(Object js : nameArray){ JSONObject json = (JSONObject)js; System.out.println(json.get("date")); } } catch (JSONException e) { e.printStackTrace(); } } void main(String[] args) { String s = "[{"date":"2012-04-23","activity":"gym"},{"date":"2012-04-24","activity":"walking"}]"; parseProfilesJson(s); } }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读