天天看點

list動态添加資料被覆寫問題

public static void test(Map<String,Object> condition){
		JSONObject json = new JSONObject();
		List<OfferBean> offerList =  offerService.getListByCondition(condition);
		List<Object> list = new ArrayList<Object>();
		if(offerList!=null&&offerList.size()>0){
			for(int i=0;i<offerList.size();i++){
				OfferBean bean = offerList.get(i);
				UserBean user = userService.getBeanByUid(bean.getUid());
				json.put("id",bean.getId() );
				json.put("productname",bean.getProductname() );
				json.put("cityname",bean.getCityname() );
				json.put("time",bean.getTime() );
				json.put("address",bean.getCityname() );
				json.put("grade",user.getGrade() );
				list.add(json);
			}
		}
	}
           

List<Object>動态添加json資料時,List中json對象内容被覆寫。

原因:

 建立JSONObject放到for循環外了,隻有一個json對象,位址一樣,内容更換時所有對象内容都被覆寫。

解決方法:

 把建立JSONObject放到for循環内。