后台接收json对象

来源:互联网 发布:百度搜索引擎优化指南 编辑:程序博客网 时间:2024/05/21 22:53

需求:后台接收json对象

js代码如下:

function ss(){var sss = JSON.stringify({"datas":[{"name":"小红", "password":"12231123"},{"name":"小黑s", "password":"12231123"}],              "data":{"orderNumber":"123222", "name":"三星"}});$.ajax({        type:'post',        url: path+'/RoadTestController/saveRoadTest.do',  dataType:'json',  async: true,data: {"date":sss},success:function(result){}});}
controller代码如下:
 */@RequestMapping("saveRoadTest")public ModelAndView saveRoadTest(HttpServletRequest request,HttpServletResponse response){String jsonStr = request.getParameter("date");JSONObject RoadTest = JSONObject.fromObject(jsonStr).getJSONObject("data");JSONArray RoadTestDetails = JSONObject.fromObject(jsonStr).getJSONArray("datas");int result = 0;String msg = "";if(RoadTestDetails.size()!=0){List<RoadTestDetails> detailsList = new ArrayList<RoadTestDetails>();for(int i=0;i<RoadTestDetails.size(); i++){JSONObject jsonJ = RoadTestDetails.getJSONObject(i);RoadTestDetails rtd= new RoadTestDetails();rtd.setName(jsonJ.getInt("name"));rtd.setPassword(jsonJ.getString("password"));}boolean istrue = appService.insertRoadTestDetails(detailsList);if(istrue){result = 1;if(RoadTest.size()!=0){List<RoadTest> list = new ArrayList<RoadTest>();RoadTest rt = new RoadTest();rt.setOrderNumber(RoadTest.getString("orderNumber"));rt.setName(RoadTest.getString("name"));boolean isTrue = appService.insertRoadTest(list);if(isTrue){ result = 1;}else{ result = 0; msg = "上传数据失败!";}}}else{msg = "上传数据失败!";}}else{msg = "上传数据为空!";}MappingJackson2JsonView view = new MappingJackson2JsonView();Map<String, Object> attributes = new HashMap<String, Object>();attributes.put("result", result);attributes.put("message", msg);view.setAttributesMap(attributes);ModelAndView model = new ModelAndView();model.setView(view);return model;}


0 0
原创粉丝点击