20200804

本文详细介绍了在Java中使用JSONObject进行数据操作的方法,包括如何处理键值对的添加及异常捕获,同时展示了如何利用日志记录系统错误,避免使用可能引起程序锁死的e.printStackTrace()。
JSONObject.java
-------------------
JSONObject xx = new JSONObject();
try{
	xx.put(key,value);

}catch(JSONObject e){
	e.printStackTrace;   <!--查看完整错误信息 只能在控制台输出-->
}

<!--e.printStackTrace()要尽量少用,可能会导致锁死-->




log.info("xxx");
-------------------
private static final Log log = Logs.get();

JSONObject xx = new JSONObject();
try{
	xx.put(key,value);

}catch(JSONObject e){
	log.info("System Error",e);
}




public class DressPartitionServiceImpl implements DressPartitionService{
	private static final LOGGER = Logger.getLogger(User.calss);
	
	private DressPartitionDao dressPartitionDao;

	private IntroduceDao introduceDao;

	private CollectDao collectDao;
  
	public JSONObject getDressPartitionList(Map<String,String> param){
			<!--Map<String, String> params定义的是一个存放map键值对的集合,键和值的类型都是String类型的 集合的对象是params参数-->
		String csmId = param.get("csmId");
		JSONObject jsonUser = new JSONObject();

		try{
			List<HashMap<String,Object>> dresspartitionList = dressPartitionDao.getDressPartition();
			for(int i=0;i<dresspartitionList.size();i++){  <!--.size()就是获取到ArrayList中存储的对象的个数-->
				String substring = dresspartitionList.get(i).get("textimagesIds").toString();
						<!--list.get(i)获得list中第i个对象 .get("textimagesIds")获得list中第i个对象的textimagesIds  .toString()将textimagesIds转换成字符串类型 -->
				String[] spilt = substring.spilt(",");
						<!--.spilt(",") 将一个字符串按照","分割为子字符串,然后将结果作为字符串数组返回-->
				List<HashMap<String,Object>> imagesList = new ArrayList<HashMap<String,Object>>();
				HashMap<String,Object> map = new HashMap<String,Object>();

				for(String s:spilt){
					if(StringUtil.isEmpty(s)||s.equals("null")){
						<!-- StringUtil方法的操作对象是java.lang.String类型的对象 isEmpty等价于str==null||str.length==0-->
						<!-- .equals比较字符串所包含的内容是否相同 ==比较两个变量本身的值,即两个对象在内存中的首地址-->
						<!-- 防止输入为空的错误 -- >
						continue;
					}
					String imagseUrl=introduceDao.getIntroduceById(Integer.valueOf(s));

					map.put("imagseUrl",imagseUrl);
					imagesList.add(map);
				}
				dresspartitionList.get(i).put("textimagesUrl",imagesList);

				int count = collectDao.getCollect(Integer.valueOf(csmId),Integer.valueOf(dresspartitionList.get(i).get("id").toString()));
				if(count>0){
					dresspartitionList.get(i).put("state","true");
				}else{
					dresspartitionList.get(i).put("state","false");
				}
			}
		}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值