关于json的解析

json在javaweb中是一个很好的传输数据的工具,封装的数据一步了然

所需要的jar包下载地址:json-jar包(点击0积分下载)

js的代码

function saveOrder(){
	var postMethod ;//邮寄方式
	
	if($("#post_method").val==1){
		postMethod = 1;
	}else{
		$("[name='company']").each(function(){
			if(this.checked){
				postMethod = this.id.substr(this.id.indexOf("_")+1,this.id.length);
			}
		});
	}
	
	var postInfo;//邮寄信息
	var index = -1;
	$("[name='radio2']").each(function(){//检查是否是用的历史邮寄信息
		if(this.checked){
			index = this.id.substr(this.id.indexOf("_")+1,this.id.length);
		}
	});
	
	if(index!=-1){
		postInfo = {
				"address":$("#address_"+index).html(),
				"city":$("#city"+index).html(),
				"county":$("#seat"+index).html(),
				"linkManName":$("#postLinkManName"+index).html(),
				"linkManPhone":$("#linkManTel"+index).html(),
				"linkManSex":null,
				"postCode":null,
				"postMethod":postMethod,//邮寄方式
				"province":$("#province"+index).html(),
				"remark":$("#remark").html()
			};
	}else{
		postInfo = {
				"address":$("#address").val(),
				"city":$("#city").val(),
				"county":$("#seat").val(),
				"linkManName":$("#postLinkManName").html(),
				"linkManPhone":$("#linkManTel").html(),
				"linkManSex":$("#sex").val(),
				"postCode":$("#postCode").html(),
				"postMethod":postMethod,//邮寄方式
				"province":$("#province").val(),
				"remark":$("#remark").html()
			};
	}
	var isPost = false;
	$("[name='isPost']").each(function(){//检查是否需要邮寄
		if(this.checked&&this.value=="1"){
			isPost = true;
		}
	});

	if(!isPost){
		postInfo = {};
	}
	
	var travellers = new Array();
	
	for (var i = 0;i < $("#tabCount").val();i++) {
		var travellerType = $("#travellerType"+i).val();
		var tickets = new Array();//重置客票,防止信息未清除
		for(var j = 0;j<$("#priceCount").val();j = j + 2){//客票信息处理
			var ticketUnites = [{"arriveCity":$("#departAirport"+j).html(),								//票联信息
								 "arriveTime":$("#departDate"+j).html()+" "+$("#arriveTime"+j).html(),
								 "departCity":$("#arriveAirport"+j).html(),
								 "departTime":$("#flightDate"+j).html()+" "+$("#departTime"+j).html(),
								 "flightNum":$("#flightNum"+j).html(),
								 "serialNum":1,
								 "shippingSpace":$("#shippingSpace"+j).html()},
								 {"arriveCity":$("#departAirport"+j).html(),
								  "arriveTime":$("#departDate"+j).html()+" "+$("#arriveTime"+j).html(),
								  "departCity":$("#arriveAirport"+j).html(),
								  "departTime":$("#flightDate"+j).html()+" "+$("#departTime"+j).html(),
								  "flightNum":$("#flightNum"+j).html(),
								  "serialNum":2,
								  "shippingSpace":$("#shippingSpace"+j).html()}];
			tickets[j]={
				"airBuildCost":$("#airBuildCost"+j+"_"+travellerType).html()+$("#airBuildCost"+(j+1)+"_"+travellerType).html(),
				"airOilCost":$("#airOilCost"+j+"_"+travellerType).html()+$("#airOilCost"+(j+1)+"_"+travellerType).html(),
				"insuranceCost":0,
				"isReturn":false,
				"ticketPrice":$("#ticketPrice"+j+"_"+travellerType).html()+$("#ticketPrice"+(j+1)+"_"+travellerType).html(),
				"ticketUnites":JSON.stringify(ticketUnites)
			};
		}
		travellers[i] = {//旅客
			"birthDate":$("#birthDate"+i).html(),
			"cnFirstName":null,
			"cnFullName":$("#cnFullName"+i).val(),
			"cnLastName":null,
			"credCode":$("#credCode"+i).html(),
			"credType":$("#credType"+i).html(),
			"customerCode":$("#customerCode"+i).html(),
			"enFirstName":null,
			"enFullName":null,
			"enLastName":null,
			"isBuyInsurance":true,
			"memberCardCode":$("#memberCardCode"+i).html(),
			"memberCardType":$("#memberCardType"+i).html(),
			"parendName":$("#parendName"+i).val(),
			"parentId":null,
			"phone":$("#phone"+i).html(),
			"travellerType":travellerType,
			"tickets":JSON.stringify(tickets)
		};
		
	}
	var legInfoes = new Array();
	var tickets = new Array();
	var salesTicketPrice = new Array();
	
	for (var i = 0 ; i < $("#fCount").val() ; i++) {
		legInfoes[i] = {//航班信息
			"aircraftType" : $("#aircraftType" + i).html(),
			"arriveAirport" : $("#to" + i).html(),
			"arriveTime":$("#arriveTime" + i).html(),
			"changeStop" : $("#changeStop" + i).html(),
			"departAirport":$("#departAirport" + i).html(),
			"departTime":$("#departTime" + i).html(),
			"flightDate":$("#flightDate"+i).html(),
			"flightNum":$("#flightNum" + i).html(),
			"meal":$("#meal" + i).html(),
			"shippingSpace":$("#shippingSpace" + i).html()
			};
	};
	var order = {"airBuildCost" : $("#airBuildCost").html(),//订单
				 "airOilCost" : $("#airOilCost").html(),
				 "insuranceCost" : $("#insuranceCost").html(),
				 "isReduceOrder":false,
				 "linkManBackPhone":$("#linkManBackPhone").html(),
				 "linkManName":$("#linkManName").html(),
				 "linkManPhone":$("#linkManPhone").html(),
				 "memberCardCode":$("#membershipCardCode1").html(),
				 "memberCredCode":$("#credentialsCode1").html(),
				 "memberName":$("#travellerChFullName1").html(),
				 "orderStatus":1,
				 "orderType":1,
				 "privilegeCost":$("#privilegeCost").html(),
				 "reserveDate":(($("#reserveTicketdate").html()=='')?null:$("#reserveTicketdate").html()),
				 "reserveTime":$("#reserveTicketTime").html(),
				 "ticketPrice":$("#ticketPrice").html(),
				 "totalPayCost":$("#totalPayCost").html()};
	var obj = {"order" : JSON.stringify(order),
			   "salesLegInfoes" : JSON.stringify(legInfoes),
			   "postInfo" : JSON.stringify(postInfo),
			   "travellers" : JSON.stringify(travellers)
			  };
	$.post("${ctx}/response/save/",obj,function(data){
		alert(data.flag);
	});
	
	//$(".dialog").dialog("open");
}
</script>
后台得到的数据大概是:
{"order":
"{\"airBuildCost\":\"300\",\"airOilCost\":\"420\",
\"insuranceCost\":\"100\",\"isReduceOrder\":false,
\"linkManBackPhone\":\"fref\",\"linkManName\":\"qfqf\",
\"linkManPhone\":\"fer\",\"orderStatus\":1,\"orderType\":1,
\"privilegeCost\":\"1000\",\"reserveDate\":\"\",
\"reserveTime\":\"21\",\"ticketPrice\":\"2410\",
\"totalPayCost\":\"10000\"}",
"salesLegInfoes":"[
	{\"aircraftType\":\"77A\",\"arriveTime\":\"14:00\",\"changeStop\":\"0\",\"departAirport\":\"CAN\",\"departTime\":\"11:00\",\"flightDate\":\"2011-5-20\",\"flightNum\":\"HU3101\",\"meal\":\"正餐\",\"shippingSpace\":\"\"},
	{\"aircraftType\":\"77A\",\"arriveTime\":\"14:00\",\"changeStop\":\"0\",\"departAirport\":\"CAN\",\"departTime\":\"11:00\",\"flightDate\":\"2011-5-20\",\"flightNum\":\"HU3101\",\"meal\":\"正餐\",\"shippingSpace\":\"\"},
	{\"aircraftType\":\"77A\",\"arriveTime\":\"14:00\",\"changeStop\":\"0\",\"departAirport\":\"CAN\",\"departTime\":\"11:00\",\"flightDate\":\"2011-5-20\",\"flightNum\":\"HU3101\",\"meal\":\"正餐\",\"shippingSpace\":\"\"}]",
"postInfo":"{}",
"travellers":"[
	{\"birthDate\":\"2013-07-01\",\"cnFirstName\":null,\"cnFullName\":\"\\n  \\t\\t\\t\\n  \\t\\t\\t\\tfewdfwedwefgreg\\n  \\t\\t\\t\\t\\n  \\t\\t\\t\\t\\n  \\t\\t\\t\\n  \\t\\t\\t \",\"cnLastName\":null,\"credCode\":\"fewf\",\"customerCode\":\"fwerf\",\"enFirstName\":null,\"enFullName\":null,\"enLastName\":null,\"isBuyInsurance\":true,\"memberCardCode\":\"qq\",\"memberCardType\":\"\",\"parendName\":\"\",\"parentId\":null,\"phone\":\"fwe\",\"travellerType\":\"\",
		\"tickets\":\"[
			{\\\"airBuildCost\\\":null,\\\"airOilCost\\\":null,\\\"insuranceCost\\\":0,\\\"isReturn\\\":false,\\\"ticketPrice\\\":null,
					\\\"ticketUnites\\\":\\\"[
							{\\\\\\\"arriveCity\\\\\\\":\\\\\\\"CAN\\\\\\\",\\\\\\\"arriveTime\\\\\\\":\\\\\\\"2011-5-21 14:00\\\\\\\",\\\\\\\"departCity\\\\\\\":\\\\\\\"PEK\\\\\\\",\\\\\\\"departTime\\\\\\\":\\\\\\\"2011-5-20 11:00\\\\\\\",\\\\\\\"flightNum\\\\\\\":\\\\\\\"HU3101\\\\\\\",\\\\\\\"serialNum\\\\\\\":1,\\\\\\\"shippingSpace\\\\\\\":\\\\\\\"\\\\\\\"},
							{\\\\\\\"arriveCity\\\\\\\":\\\\\\\"CAN\\\\\\\",\\\\\\\"arriveTime\\\\\\\":\\\\\\\"2011-5-21 14:00\\\\\\\",\\\\\\\"departCity\\\\\\\":\\\\\\\"PEK\\\\\\\",\\\\\\\"departTime\\\\\\\":\\\\\\\"2011-5-20 11:00\\\\\\\",\\\\\\\"flightNum\\\\\\\":\\\\\\\"HU3101\\\\\\\",\\\\\\\"serialNum\\\\\\\":2,\\\\\\\"shippingSpace\\\\\\\":\\\\\\\"\\\\\\\"}
							]\\\"},null,
			{\\\"airBuildCost\\\":null,\\\"airOilCost\\\":null,\\\"insuranceCost\\\":0,\\\"isReturn\\\":false,\\\"ticketPrice\\\":null,
					\\\"ticketUnites\\\":\\\"[
							{\\\\\\\"arriveCity\\\\\\\":\\\\\\\"CAN\\\\\\\",\\\\\\\"arriveTime\\\\\\\":\\\\\\\"2011-5-21 14:00\\\\\\\",\\\\\\\"departCity\\\\\\\":\\\\\\\"PEK\\\\\\\",\\\\\\\"departTime\\\\\\\":\\\\\\\"2011-5-20 11:00\\\\\\\",\\\\\\\"flightNum\\\\\\\":\\\\\\\"HU3101\\\\\\\",\\\\\\\"serialNum\\\\\\\":1,\\\\\\\"shippingSpace\\\\\\\":\\\\\\\"\\\\\\\"},
							{\\\\\\\"arriveCity\\\\\\\":\\\\\\\"CAN\\\\\\\",\\\\\\\"arriveTime\\\\\\\":\\\\\\\"2011-5-21 14:00\\\\\\\",\\\\\\\"departCity\\\\\\\":\\\\\\\"PEK\\\\\\\",\\\\\\\"departTime\\\\\\\":\\\\\\\"2011-5-20 11:00\\\\\\\",\\\\\\\"flightNum\\\\\\\":\\\\\\\"HU3101\\\\\\\",\\\\\\\"serialNum\\\\\\\":2,\\\\\\\"shippingSpace\\\\\\\":\\\\\\\"\\\\\\\"}
							]\\\"}]\"},
	{\"birthDate\":\"2013-07-01\",\"cnFirstName\":null,\"cnFullName\":\"\\n  \\t\\t\\t\\n  \\t\\t\\t\\tferwfer\\n  \\t\\t\\t\\t\\n  \\t\\t\\t\\t\\n  \\t\\t\\t\\n  \\t\\t\\t \",\"cnLastName\":null,\"credCode\":\"\",\"customerCode\":\"\",\"enFirstName\":null,\"enFullName\":null,\"enLastName\":null,\"isBuyInsurance\":true,\"memberCardCode\":\"\",\"memberCardType\":\"\",\"parendName\":\"\",\"parentId\":null,\"phone\":\"\",\"travellerType\":\"\",
		\"tickets\":\"[
			{\\\"airBuildCost\\\":null,\\\"airOilCost\\\":null,\\\"insuranceCost\\\":0,\\\"isReturn\\\":false,\\\"ticketPrice\\\":null,
					\\\"ticketUnites\\\":\\\"[
							{\\\\\\\"arriveCity\\\\\\\":\\\\\\\"CAN\\\\\\\",\\\\\\\"arriveTime\\\\\\\":\\\\\\\"2011-5-21 14:00\\\\\\\",\\\\\\\"departCity\\\\\\\":\\\\\\\"PEK\\\\\\\",\\\\\\\"departTime\\\\\\\":\\\\\\\"2011-5-20 11:00\\\\\\\",\\\\\\\"flightNum\\\\\\\":\\\\\\\"HU3101\\\\\\\",\\\\\\\"serialNum\\\\\\\":1,\\\\\\\"shippingSpace\\\\\\\":\\\\\\\"\\\\\\\"},
							{\\\\\\\"arriveCity\\\\\\\":\\\\\\\"CAN\\\\\\\",\\\\\\\"arriveTime\\\\\\\":\\\\\\\"2011-5-21 14:00\\\\\\\",\\\\\\\"departCity\\\\\\\":\\\\\\\"PEK\\\\\\\",\\\\\\\"departTime\\\\\\\":\\\\\\\"2011-5-20 11:00\\\\\\\",\\\\\\\"flightNum\\\\\\\":\\\\\\\"HU3101\\\\\\\",\\\\\\\"serialNum\\\\\\\":2,\\\\\\\"shippingSpace\\\\\\\":\\\\\\\"\\\\\\\"}
							]\\\"},null,         
			{\\\"airBuildCost\\\":null,\\\"airOilCost\\\":null,\\\"insuranceCost\\\":0,\\\"isReturn\\\":false,\\\"ticketPrice\\\":null,
					\\\"ticketUnites\\\":\\\"[
							{\\\\\\\"arriveCity\\\\\\\":\\\\\\\"CAN\\\\\\\",\\\\\\\"arriveTime\\\\\\\":\\\\\\\"2011-5-21 14:00\\\\\\\",\\\\\\\"departCity\\\\\\\":\\\\\\\"PEK\\\\\\\",\\\\\\\"departTime\\\\\\\":\\\\\\\"2011-5-20 11:00\\\\\\\",\\\\\\\"flightNum\\\\\\\":\\\\\\\"HU3101\\\\\\\",\\\\\\\"serialNum\\\\\\\":1,\\\\\\\"shippingSpace\\\\\\\":\\\\\\\"\\\\\\\"},
							{\\\\\\\"arriveCity\\\\\\\":\\\\\\\"CAN\\\\\\\",\\\\\\\"arriveTime\\\\\\\":\\\\\\\"2011-5-21 14:00\\\\\\\",\\\\\\\"departCity\\\\\\\":\\\\\\\"PEK\\\\\\\",\\\\\\\"departTime\\\\\\\":\\\\\\\"2011-5-20 11:00\\\\\\\",\\\\\\\"flightNum\\\\\\\":\\\\\\\"HU3101\\\\\\\",\\\\\\\"serialNum\\\\\\\":2,\\\\\\\"shippingSpace\\\\\\\":\\\\\\\"\\\\\\\"}
							]\\
		    \"}
		]\
	"}
	]"
	}

可以看到,这里的json是很复杂的

解析如下

/**
  * @description:订单的增加
  * @author: luoxiaoyang
  * @createDate: 2013-7-16
  * @param request
  * @return:
  */
 @SuppressWarnings("unchecked")
 @RequestMapping(value = "/save/", method = RequestMethod.POST 
   ,headers = "X-Requested-With=XMLHttpRequest")
 @ResponseBody
 public ResultData save(HttpServletRequest request) {
  // 得到字符串形式的数据
  String orderStr = request.getParameter("order");
  String salesLegInfoesStr = request.getParameter("salesLegInfoes");
  String postInfoStr = request.getParameter("postInfo");
  String travellersStr = request.getParameter("travellers");

  // 格式化json的时间,否则时间无法对应的保存
  JSONUtils.getMorpherRegistry().registerMorpher(
    new DateMorpher(new String[] { "yyyy-MM-dd" }));
  SalesOrder order = (SalesOrder) JSONObject.toBean(JSONObject
    .fromObject(orderStr), SalesOrder.class);
  List<SalesLegInfo> salesLegInfos = (List<SalesLegInfo>) JSONArray
    .toCollection(JSONArray.fromObject(salesLegInfoesStr),
      SalesLegInfo.class);
  SalesPostInfo postInfo = (SalesPostInfo) JSONObject.toBean(JSONObject
    .fromObject(postInfoStr), SalesPostInfo.class);
  JSONArray travellersJsonArr = JSONArray.fromObject(travellersStr);// 旅客信息
  System.out.println("travellersJsonArr:"+travellersJsonArr);
  // 票的信息
  List<SalesTraveller> travellers = new ArrayList<SalesTraveller>();
  System.out.println(travellersJsonArr.size());
  for (int i = 0; i < travellersJsonArr.size(); i++) {
   JSONObject travellerJson = JSONObject.fromObject(travellersJsonArr
     .get(i));
   System.out.println("salesTraveller:"+travellerJson.discard(""));
   SalesTraveller salesTraveller = (SalesTraveller) JSONObject.toBean(
     JSONObject.fromObject(travellerJson.discard("tickets")),
     SalesTraveller.class);

   JSONArray ticketsJsonArr = JSONArray.fromObject(travellerJson
     .get("tickets"));

   List<SalesTicket> tickets = new ArrayList<SalesTicket>();
   for (int j = 0; j < ticketsJsonArr.size(); j++) {
    JSONObject ticketJson = JSONObject.fromObject(ticketsJsonArr
      .get(j));
    SalesTicket ticket = (SalesTicket) JSONObject.toBean(JSONObject
      .fromObject(ticketJson.discard("ticketUnites")),
      SalesTicket.class);

    List<SalesTicketUnite> ticketUnite = (List<SalesTicketUnite>) JSONArray
      .toCollection(JSONArray.fromObject(ticketJson
        .get("ticketUnites")), SalesTicketUnite.class);

    ticket.setTicketUnites(ticketUnite);
    tickets.add(ticket);
   }
   salesTraveller.setSalesTickets(tickets);
   travellers.add(salesTraveller);
  }

  order.setSalesLegInfos(salesLegInfos);
  order.setSalesPostInfo(postInfo);
  order.setSalesTravellers(travellers);
  orderService.save(order);
  System.out.println(JSONObject.fromObject(order).toString());
  return new ResultData(true);
 }


你可能感兴趣的:(关于json的解析)