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");
}
}
}
}
}
20200804
最新推荐文章于 2025-12-17 19:02:04 发布
本文详细介绍了在Java中使用JSONObject进行数据操作的方法,包括如何处理键值对的添加及异常捕获,同时展示了如何利用日志记录系统错误,避免使用可能引起程序锁死的e.printStackTrace()。
951

被折叠的 条评论
为什么被折叠?



