json的解析报错

发现一个问题  json解析会异常第一次用的net.sf.json.JSONObject的包,解析一个不存在的键,getString()会发生异常,而用alilibaba的JSONObject则不会报错,最多只是一个null 

List<String> seedBrokers =new ArrayList<String>();
		net.sf.json.JSONObject sss = new net.sf.json.JSONObject();
		sss.put("aa", "fdsf");
		com.alibaba.fastjson.JSONObject parseObject = JSON.parseObject(sss.toString());
		String str = parseObject.getString("aa");
		System.out.println(str==null);
		System.out.println("---------------");
		
		
		String st="{\"actiontype\":\"heartbeat\"}";
//		 JSONObject jsonObject = JSONObject.fromObject(st);
		 com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(sss.toString());
		 String hotelId = jsonObject.getString("hotelid");
		 String aa = jsonObject.getString("aa");
			String getroomid = jsonObject.getString("roomid");
		 System.out.println(hotelId+"\r\n"+getroomid+"==-="+aa);
		 JSONObject sssf=JSONObject.fromObject(jsonObject);
		 System.out.println("转换="+sssf);

以上方法有解析的,有net的jsonObject  与  alibaba的jsonObject 互转的过程,为什么会有这个问题呢,因为用的netty 只要这个socket 过程有一个try catch 他就会 到 handler的 exceptionCaught()  方法,然后就会发邮件提醒,所以感觉这是个误操作,因为要兼容 v1  v2版本,他们的字段即键 是不相同的所以有时候是误报,这样就引申出来这个互转了,以后的话尽量用阿里的 json解析,兼容削微好一点,fastjson 还是相当好用的。

 

 

おすすめ

転載: blog.csdn.net/weixin_41086086/article/details/86544315