JQuery Ajax is an easy way to pass a single JSON object to the background, where a JSON array with multiple JSON objects is passed into the Java background and explains how the JSON array is parsed by Java.
1. JS Code
var relationarrays=new Array ();
Get people information for all groups grid data
var allgrid= $ (". UserGrid");
for (Var i=0;i<allgrid.length;i++) {
var rows=$ (Allgrid[i]). DataGrid ("GetRows");
$.each (Rows,function (I,item) {
Relationarrays.push (item);
})
}
$.ajax ({
Type: "POST",
URL: '.. /projectcontroller/addrelations ',
data:{"params": Json.stringify (Relationarrays)},
DataType: ' JSON ',
Cache:false,
Success:function (data) {
alert (data.msg);
}
});
2. Java code
@RequestMapping ("/addrelations")
public void Addrelations (httpservletrequest request,httpservletresponse Response, HttpSession session) {
String jsonstr = Request.getparameter ("params");
Store project personnel relationship information that requires insert
List<projectrelation> relationlist=new arraylist<projectrelation> ();
Projectrelation Relation=null;
Jsonarray Jsonarray = Jsonutil.parsearray (JSONSTR);
for (Object Ob:jsonarray) {
Jsonobject jobject = (jsonobject) ob;
Relation=new projectrelation ();
Relation.setprojectid (PID);
Relation.setchargemanid (Jobject.getinteger ("Chargemanid"));
Relation.setgroupid (Jobject.getinteger ("groupId"));
Relation.setuserid (Jobject.getinteger ("userId"));
Relation.setprojectrole (Jobject.getinteger ("Projectrole"));
Relationlist.add (relation);
}
Check all the existing personnel information in the project first,
int result=projectserviceimpl.saveprojectrelations (relationlist);
hashmap<string, object> map = new hashmap<string, object> ();
try {
if (Result==jsonarray.size ()) {
Map.put ("msg", "Associated information added successfully");
}
else {
Map.put ("msg", "Associated Information add error");
}
Writejsonutil.writejson (map, response);
} catch (Exception e) {
E.printstacktrace ();
Map.put ("msg", "Associated Information add error");
Writejsonutil.writejson (map, response);
}
}
3. Jsonutil Code
public static Jsonarray Parsearray (String text) {
Jsonarray Jsonarray=json.parsearray (text);
return jsonarray;
}
Please download the specific jsonutil code from this link Http://files.cnblogs.com/files/DylanZ/JsonUtil.rar
JQuery Ajax delivers JSON arrays to spring Controller