1.遍历List集合中的数据存入jsonobject中然后放入jsonArray:
public String SearchInfo(HttpServletRequest request,HttpServletResponse response){ List<NewsVO> nvlist=new ArrayList<NewsVO>(); NewsVO nv1=new NewsVO(); nv1.setId(1); nv1.setTitle("标题一"); nv1.setUrl("www.123.com"); NewsVO nv2=new NewsVO(); nv2.setId(2); nv2.setTitle("标题二"); nv2.setUrl("www.1233.com"); NewsVO nv3=new NewsVO(); nv3.setId(3); nv3.setTitle("标题三"); nv3.setUrl("www.1d3.com"); nvlist.add(nv1); nvlist.add(nv2); nvlist.add(nv3); /*for(NewsVO nvo:nvlist){ System.out.println(nvo.getId()+nvo.getTitle()); }*/ //JSON操作,存JSONArray JSONArray jsons=new JSONArray(); JSONObject json=null; for(NewsVO nv:nvlist){ json=new JSONObject(); json.put("id",nv.getId()); json.put("title",nv.getTitle()); json.put("url",nv.getUrl()); json.put("tag",nv.getTag()); jsons.add(json); } System.out.println(jsons); System.out.println(jsons.toString()); //Test request.setAttribute("name","佰佰"); return "/index"; }