`
Everyday都不同
  • 浏览: 723536 次
  • 性别: Icon_minigender_1
  • 来自: 宇宙
社区版块
存档分类
最新评论

【2016-12-30】工作笔记

阅读更多

时光飞逝,大概是2016年最后一篇博~

 

1. net.sf.json如何传递集合?

 

List<Map<String, String>> list2 = new ArrayList<Map<String,String>>();
		Map<String, String> map4 = new HashMap<String, String>();
		map4.put("id", "1");
		map4.put("pk", "1");
		list2.add(map4);
		Map<String, String> map5 = new HashMap<String, String>();
		map5.put("id", "2");
		map5.put("pk", "1");
		list2.add(map5);
		Map<String, String> map6 = new HashMap<String, String>();
		map6.put("id", "3");
		map6.put("pk", "4");
		list2.add(map6);
		Map<String, String> map7 = new HashMap<String, String>();
		map7.put("id", "4");
		map7.put("pk", null);
		list2.add(map7);
		System.out.println("想要发送的集合:" + list2);
		System.out.println("=====================================");
		
		try {
			//模拟发送
			JSONObject jsonObj = new JSONObject();
			Map<String, Object> requestMsg = new HashMap<String, Object>();
			System.out.println("发送jsonArr:" + JSONArray.fromObject(list2));
//			requestMsg.put("list", JSONArray.fromObject(list2));//把集合转成JSONArray传递
			requestMsg.put("list", list2);//直接传集合
			jsonObj.put("requestMsg", requestMsg);
			
			System.out.println("**************************");
			
			//模拟接收
			JSONObject res = JSONObject.fromObject(jsonObj.get("requestMsg"));
			String str = res.getString("list");//可以用字符串接收集合
			System.out.println("用字符串接收str:" + str);
			JSONArray arr = JSONArray.fromObject(res.get("list"));//也可以用JSONArray接收
			System.out.println("用JSAONArray接收arr:" + arr);
			
			List<Map<String, String>> ss = (List) JSONArray.toCollection(arr, Map.class);//这才是我们想要的集合
			
			System.out.println("真正想要接收的集合list:" + ss);
			
		} catch (Exception e) {
			e.printStackTrace();
		}

 打印结果:

 

想要发送的集合:[{id=1, pk=1}, {id=2, pk=1}, {id=3, pk=4}, {id=4, pk=null}]

=====================================

发送jsonArr:[{"id":"1","pk":"1"},{"id":"2","pk":"1"},{"id":"3","pk":"4"},{"id":"4","pk":null}]

**************************

用字符串接收str:[{"id":"1","pk":"1"},{"id":"2","pk":"1"},{"id":"3","pk":"4"},{"id":"4","pk":null}]

用JSAONArray接收arr:[{"id":"1","pk":"1"},{"id":"2","pk":"1"},{"id":"3","pk":"4"},{"id":"4","pk":null}]

真正想要接收的集合list:[{id=1, pk=1}, {id=2, pk=1}, {id=3, pk=4}, {id=4, pk=null}]

 

这种情况在接口的调用中传递参数和接口接收参数可能遇到,一般来说传递一个map或者String类型的参数比较简单,而传递和接收集合类型的参数就需要注意了,我也被这坑了好久保密。。

 

2、org.apache.commons.collections.CollectionUtils类的运用

之前还一直傻傻的自己去写交集和并集,差集等,用了retainAll等原始的方法。殊不知自己寡闻了,不知道有CollectionUtils这个神奇的集合工具类。

最常见的如:

List<Map<String, String>> intersection = (List<Map<String, String>>) CollectionUtils.intersection(list1, list2);
System.out.println("list1和list2的交集:" + intersection);
List<Map<String, String>> subtract = (List<Map<String, String>>) CollectionUtils.subtract(list2, intersection);
System.out.println("list2有而list1没有的差集:" + subtract);

 

3、小技巧:妙用Matcher.group来查找XML中某个节点的值,如需要在下列XML的字符串中找到<sender>节点的值。

String msgContent = "<div class='sevice_chat'><div class='chat_box'><div class='chat_01'><span class='chat_title'><sender>1990s王梅</sender>&nbsp;&nbsp;&nbsp;&nbsp;<time>2016-04-21 16:51:27</time></span><br><span><content>您好,很高兴为您服务,有什么可以帮您?</content></span><div style='display:none;'><receiver>mucdeb8b7b8@conference.openfire-test</receiver></div></div></div></div><div class='me_chat'><div class='me_box'><div class='me_01'><span class='me_title'><sender>访客3580</sender>&nbsp;&nbsp;&nbsp;&nbsp;<time>2016-04-21 16:52:19</time></span><br><span><content></content></span><div style='display:none;'><receiver>44b33218</receiver></div></div></div></div><div class='sevice_chat'><div class='chat_box'><div class='chat_01'><span class='chat_title'><sender>1990s王梅</sender>&nbsp;&nbsp;&nbsp;&nbsp;<time>2016-04-21 16:52:20</time></span><br><span><content>"
				+ "<span style=\"font-size:12px;font-weight:normal;font-style:normal;text-decoration:none;line-height:12px;font-family:微软雅黑\">请问有什么可以帮您?</span></content></span><div style='display:none;'><receiver>mucdeb8b7b8@conference.openfire-test</receiver></div></div></div></div><div class='me_chat'><div class='me_box'><div class='me_01'><span class='me_title'><sender>访客3580</sender>&nbsp;&nbsp;&nbsp;&nbsp;<time>2016-04-21 16:53:18</time></span><br><span><content><font size=\"3\"><span style=\"-webkit-text-size-adjust: auto; background-color: rgba(255, 255, 255, 0);\">你好,对市场行情走势怎么看?</span></font></content></span><div style='display:none;'><receiver>44b33218</receiver></div></div></div></div>";
		
		Matcher sender = Pattern.compile("<sender.*?>([\\s\\S]*?)</sender>").matcher(msgContent);
		while(sender.find()) {
			String str = sender.group(1);
			System.out.println("sender:" + str);
		}

 打印结果为:

sender:1990s王梅

sender:访客3580

sender:1990s王梅

sender:访客3580

 

4、后台挂起的方式启动mongo

记得要加参数fork,不然随着xshell的关闭,mongo进程也将随之被kill掉。。

如:./mongod -dbpath=../data -logpath=../log/mongodb.log -fork

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics