博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
json-lib与jackson的比较
阅读量:6137 次
发布时间:2019-06-21

本文共 6116 字,大约阅读时间需要 20 分钟。

hot3.png

<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
inMap = new HashMap
(); SalesOrder order = null; // 格式化json的时间,否则时间无法对应的保存 JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(new String[] { "yyyy-MM-dd" })); order = (SalesOrder) JSONObject.toBean( JSONObject.fromObject(orderStr), SalesOrder.class); /*List
salesLegInfos = (List
) JSONArray .toCollection(JSONArray.fromObject(salesLegInfoesStr), SalesLegInfo.class);*/ /* List
salesLegInfos = new ArrayList
(); JSONArray salesLegInfoesArr = JSONArray.fromObject(salesLegInfoesStr); for(int i= 0;i
salesFreightRates = (List
)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
travellers = new ArrayList
(); 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
tickets = new ArrayList
(); 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
ticketUnite = (List
) 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
rMap = new HashMap
(); 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
salesLegInfoList = new ArrayList
(); try { order = mapper.readValue(orderStr, SalesOrder.class); SalesPostInfo salesPostInfo = mapper.readValue(postInfoStr, SalesPostInfo.class); List
> salesLegInfoMapList = mapper.readValue(salesLegInfoesStr, List.class); for(LinkedHashMap
map: salesLegInfoMapList){ List
salesFreightRates = mapper.readValue(map.get("salesFreightRates").toString(), new TypeReference
>() {}); map.remove("salesFreightRates"); SalesLegInfo salesLegInfo = mapper.convertValue(map, SalesLegInfo.class); salesLegInfo.setSalesFreightRates(salesFreightRates); salesLegInfoList.add(salesLegInfo); } List
> travellersMapList = mapper.readValue(travellersStr, List.class); List
travellers = new ArrayList
(); for (LinkedHashMap
map: travellersMapList) { List
> ticketsMapList = mapper.readValue(map.get("tickets").toString(), List.class); List
tickets = new ArrayList
(); for (LinkedHashMap
inMap: ticketsMapList) { List
ticketUnites = mapper.readValue(inMap.get("ticketUnites").toString(), new TypeReference
>() {}); 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); }

 

转载于:https://my.oschina.net/bughope/blog/403750

你可能感兴趣的文章
下一步工作分配
查看>>
Response. AppendHeader使用大全及文件下载.net函数使用注意点(转载)
查看>>
Wait Functions
查看>>
代码描述10313 - Pay the Price
查看>>
jQuery最佳实践
查看>>
centos64i386下apache 403没有权限访问。
查看>>
vb sendmessage 详解1
查看>>
jquery用法大全
查看>>
PC-BSD 9.2 发布,基于 FreeBSD 9.2
查看>>
网卡驱动程序之框架(一)
查看>>
css斜线
查看>>
Windows phone 8 学习笔记(3) 通信
查看>>
重新想象 Windows 8 Store Apps (18) - 绘图: Shape, Path, Stroke, Brush
查看>>
Revit API找到风管穿过的墙(当前文档和链接文档)
查看>>
Scroll Depth – 衡量页面滚动的 Google 分析插件
查看>>
Windows 8.1 应用再出发 - 视图状态的更新
查看>>
自己制作交叉编译工具链
查看>>
Qt Style Sheet实践(四):行文本编辑框QLineEdit及自动补全
查看>>
[物理学与PDEs]第3章习题1 只有一个非零分量的磁场
查看>>
深入浅出NodeJS——数据通信,NET模块运行机制
查看>>