首页 > 代码库 > 页面传递Json对象,后台解析

页面传递Json对象,后台解析

使用的组件

前台组件:json2.js  来自 http://www.json.org/js.html
后台组件:json-lib-2.2.2-jdk15.jar

前台代码

1 通过页面生成json对象列表,然后转换成字符串
// 生成json对象 转字符串
	function makeTsJsonParam(){
		  var tsList = new Array();
		  var ts = {'tn_id':'','tn_trainSortie':'','tn_takeOffTime':'','tn_landingTime':'','tn_commander':'','tn_calendar':''};		
		  var list1 = $(".flightid");
		  var list2 = $(".tn_trainSortie");
		  var list3 = $(".tn_takeOffTime");
		  var list4 = $(".tn_landingTime");
		//  var list5 = $(".tn_realityLandingTime");
		  var list6 = $(".tn_commander");
		//  var list7 = $(".tn_mechanician");
		//  var list8 = $(".tn_notes");
		  var list9 = $(".tn_calendar");
		  //alert(list1.length);
		  for (var i=0;i<list1.length;i++)
		  {
			  ts = {};
			  ts.tn_id = list1[i].value;
			  ts.tn_trainSortie = list2[i].value;
			  ts.tn_takeOffTime = list3[i].value;
			  //alert(list3[i].value)
			  ts.tn_landingTime= list4[i].value;
			//  ts.tn_realityLandingTime= list5[i].value;
			  ts.tn_commander= list6[i].value;
			//  ts.tn_mechanician= list7[i].value;
			//  ts.tn_notes= list8[i].value;
			  ts.tn_calendar= list9[i].value;
			 
			  tsList[i] = ts;
		  }
		  var str = JSON.stringify( tsList );
		  return  str ;
	}	
2 把处理过的Json字符串 提交给后台
//  把json字符串传递给后台
	function saveAllTrainSortie(){
		var str = makeTsJsonParam();
		// alert(str)
		var htmlStr = "-1";
		$.ajax({  
	       url:'../train/saveAllTrainFerrySortie.do',  
	          //url:'${ctx}/index.jsp',  
	          cache:false,  
	          type:'post',  
	          async:false,
	          dataType:'html',      
	          data:{   
	        	  trainSortieListStr:str,  
	      		  contentType: "application/x-www-form-urlencoded;charset=utf-8"  
	          },  
	          success:function(html){  
	              // alert("html:"+html)  
	              htmlStr = html; 
	          }  
	      })
	      return htmlStr
   		 /*  if(htmlStr==1){
   			  alert("保存成功");
   		  } */
	}

后台代码

1 把字符串解析成 java对象  后处理。
public void saveAllTrainFerrySortie(HttpServletRequest request,HttpServletResponse response) {
			String trainSortieListStr = request.getParameter("trainSortieListStr");
			JSONArray array = JSONArray.fromObject(trainSortieListStr);
			for(int i=0;i<array.size();i++){
				Map ts=(Map)array.get(i);
				String tn_id = ""+ts.get("tn_id");
				String tn_trainSortie = ""+ts.get("tn_trainSortie");
				  
				String tn_takeOffTime = ""+ts.get("tn_takeOffTime");
				String tn_landingTime = ""+ts.get("tn_landingTime");
				  
				//	String tn_realityLandingTime = ""+ts.get("tn_realityLandingTime");
				String tn_commander = ""+ts.get("tn_commander");
				//	String tn_mechanician = ""+ts.get("tn_mechanician");
				//	String tn_notes = ""+ts.get("tn_notes");
				String tn_calendar = ""+ts.get("tn_calendar");
				trainSoriteFerryService.saveTrainSortie(tn_id, tn_trainSortie, tn_takeOffTime, 
						tn_landingTime, tn_commander, tn_calendar);
				  
			}
			try {  
	            response.setCharacterEncoding("UTF-8");  
	            PrintWriter pw = response.getWriter();  
	            pw.write("1");  
	            pw.flush();  
	            pw.close();  
	        } catch (IOException e) {  
	            e.printStackTrace();  
	        }  
		}




页面传递Json对象,后台解析