json-lib与jackson的比较

<script type="text/javascript">
(function(){
var p = {
url:location.href,
to:'qqmail',
desc:'', /*默认分享理由(可选)*/
summary:'',/*摘要(可选)*/
title:'',/*分享标题(可选)*/
site:'',/*分享来源 如:腾讯网(可选)*/
pics:'' /*分享图片的路径(可选)*/
};
var s = [];
for(var i in p){
s.push(i + '=' + encodeURIComponent(p[i]||''));
}
document.write(["<a target='_blank' ", 'href="http://mail.qq.com/cgi-bin/qm_share?', s.join("&"), '"', ' style="cursor:pointer;text-decoration:none;outline:none"><img src="http://rescdn.qqmail.com/zh_CN/htmledition/images/function/qm_open/ico_share_02.png"/></a>'].join(""));
})();
</script>

 

<a target="_blank" href="http://mail.qq.com/cgi-bin/qm_share?t=qm_mailme&email=HXB8bmxoeHFyXXtyZXB8dHEzfnJw" style="text-decoration:none;"><img src="http://rescdn.qqmail.com/zh_CN/htmledition/images/function/qm_open/ico_mailme_12.png"/></a>

 

json-lib的解析

 

public ResultData save1(HttpServletRequest request) {
		// 得到字符串形式的数据
		String orderStr = JsonUtils.formatStr(request.getParameter("order"));
		String salesLegInfoesStr = request.getParameter("salesLegInfoes");
		String postInfoStr = request.getParameter("postInfo");
		String travellersStr = request.getParameter("travellers");
		Map<String, Object> inMap = new HashMap<String, Object>();
		SalesOrder order = null;
		// 格式化json的时间,否则时间无法对应的保存
		JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(new String[] { "yyyy-MM-dd" }));
		order = (SalesOrder) JSONObject.toBean(
				JSONObject.fromObject(orderStr), SalesOrder.class);
		
		
		/*List<SalesLegInfo> salesLegInfos = (List<SalesLegInfo>) JSONArray
				.toCollection(JSONArray.fromObject(salesLegInfoesStr),
						SalesLegInfo.class);*/
		/*
		List<SalesLegInfo> salesLegInfos = new ArrayList<SalesLegInfo>();
		JSONArray salesLegInfoesArr = JSONArray.fromObject(salesLegInfoesStr);
		
		for(int i= 0;i<salesLegInfoesArr.size();i++){
			JSONObject salesLegInfoeJson = (JSONObject)salesLegInfoesArr.get(i);
			
			SalesLegInfo salesLegInfo = (SalesLegInfo)JSONObject.toBean((JSONObject.fromObject(salesLegInfoesArr.get(i))).discard("salesFreightRates"),SalesLegInfo.class);
			
			List<SalesFreightRate> salesFreightRates = (List<SalesFreightRate>)JSONArray
			.toCollection(JSONArray.fromObject(salesLegInfoeJson.get("salesFreightRates")),
					SalesFreightRate.class);
			salesLegInfo.setSalesFreightRates(salesFreightRates);
			salesLegInfos.add(salesLegInfo);
		}
		
		
		SalesPostInfo postInfo = (SalesPostInfo) JSONObject.toBean(
				JSONObject.fromObject(postInfoStr), SalesPostInfo.class);
		JSONArray travellersJsonArr = JSONArray.fromObject(travellersStr);// 旅客信息
		// 票的信息
		List<SalesTraveller> travellers = new ArrayList<SalesTraveller>();
		for (int i = 0; i < travellersJsonArr.size(); i++) {
			JSONObject travellerJson = (JSONObject)travellersJsonArr.get(i);
			SalesTraveller salesTraveller = (SalesTraveller) JSONObject.toBean(
					JSONObject.fromObject(JsonUtils.formatStr((JSONObject.fromObject(travellersJsonArr.get(i))).discard("tickets").toString())),
					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));
				if(!"null".equals(ticketJson.toString())&&ticketJson!=null&&!"".equals(ticketJson)){
					SalesTicket ticket = (SalesTicket) JSONObject.toBean(JSONObject
							.fromObject(JsonUtils.formatStr(ticketJson.discard("ticketUnites").toString())),
							SalesTicket.class);

					List<SalesTicketUnite> ticketUnite = (List<SalesTicketUnite>) JSONArray
							.toCollection(JSONArray.fromObject(((JSONObject)ticketsJsonArr
									.get(j)).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);
		salesOrderService.save(order);
		inMap.put("order", order);
		return new ResultData(inMap);
	}


jackson的解析

    public ResultData save(HttpServletRequest request) {
        // 得到字符串形式的数据
        Map<String, Object> rMap = new HashMap<String, Object>();
        String orderStr = JsonUtils.formatStr(request.getParameter("order"));
        String salesLegInfoesStr = request.getParameter("salesLegInfoes");
        String postInfoStr = request.getParameter("postInfo");
        String travellersStr = request.getParameter("travellers");
        ObjectMapper mapper= new ObjectMapper();
        mapper.getSerializationConfig().setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
        SalesOrder order = null;
        List<SalesLegInfo> salesLegInfoList = new ArrayList<SalesLegInfo>();
        try {
            order = mapper.readValue(orderStr, SalesOrder.class);
            SalesPostInfo salesPostInfo = mapper.readValue(postInfoStr, SalesPostInfo.class);
            
            List<LinkedHashMap<String, Object>> salesLegInfoMapList = mapper.readValue(salesLegInfoesStr, List.class);
            for(LinkedHashMap<String, Object> map: salesLegInfoMapList){
                List<SalesFreightRate> salesFreightRates = mapper.readValue(map.get("salesFreightRates").toString(),
                        new TypeReference<List<SalesFreightRate>>() {});
                map.remove("salesFreightRates");
                SalesLegInfo salesLegInfo = mapper.convertValue(map, SalesLegInfo.class);
                salesLegInfo.setSalesFreightRates(salesFreightRates);
                salesLegInfoList.add(salesLegInfo);
            }
            List<LinkedHashMap<String, Object>> travellersMapList = mapper.readValue(travellersStr, List.class);
            List<SalesTraveller> travellers = new ArrayList<SalesTraveller>();
            
            for (LinkedHashMap<String, Object> map: travellersMapList) {
                List<LinkedHashMap<String, Object>> ticketsMapList = mapper.readValue(map.get("tickets").toString(), List.class);
                List<SalesTicket> tickets = new ArrayList<SalesTicket>();
                for (LinkedHashMap<String, Object> inMap: ticketsMapList) {
                    List<SalesTicketUnite> ticketUnites = mapper.readValue(inMap.get("ticketUnites").toString(),
                            new TypeReference<List<SalesTicketUnite>>() {});
                    inMap.remove("ticketUnites");
                    SalesTicket ticket = mapper.convertValue(inMap, SalesTicket.class);
                    ticket.setTicketUnites(ticketUnites);
                    tickets.add(ticket);
                }
                map.remove("tickets");
                SalesTraveller salesTraveller = mapper.convertValue(map, SalesTraveller.class);
                salesTraveller.setSalesTickets(tickets);
                travellers.add(salesTraveller);
            }
            order.setSalesLegInfos(salesLegInfoList);
            order.setSalesPostInfo(salesPostInfo);
            order.setSalesTravellers(travellers);
            salesOrderService.save(order);
            rMap.put("order", order);
        } catch (JsonParseException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new ResultData(rMap);
    }

 

你可能感兴趣的:(java,json)