有一对多关系时,后台处理JSON应注意的问题
import net.sf.json.JsonConfig;
import net.sf.json.util.PropertyFilter;
import net.sf.json.JSONObject;
import net.sf.json.JSONArray;
/**
* 获取spid下的内容分类
* @param request
* @param response
* @return
* @throws Exception
*/
@RequestMapping("getContentBySpID")
public String getContentBySpID(HttpServletRequest request,HttpServletResponse response)throws Exception{
String spid = request.getParameter("spid");
List<SpInfo> list = spInfoService.getBySpID(spid);
List<SpInfoClassify> spClassList = list.get(0).getSpInfoClassify();
List<Classify> classList = new ArrayList<Classify>();
for(int i=0;i<spClassList.size();i++){
Classify classify = new Classify();
classify = classifyService.findClassifyBySerialnumber(spClassList.get(i).getClassifyid());
classList.add(classify);
}
//配置bean中有级联关系的JSon,过滤掉关系表
JsonConfig config = new JsonConfig();
config.setJsonPropertyFilter(new PropertyFilter(){
public boolean apply(Object source, String name, Object value) {
if(name.equals("spInfoClassify") || name.equals("spinfo") || name.equals("classifyList") || name.equals("classify")) {
return true;
} else {
return false;
}
}
}); PrintWriter out = response.getWriter();
JSONArray jsonArr = JSONArray.fromObject(classList, config);//
这里是两个参数,过滤掉一对多的实体 JSONObject json=null;
json = new JSONObject();
System.out.println(jsonArr);
out.print(jsonArr);
json.put("jsonArr", jsonArr);
return null;
}