服务器端:
引入相应的包:json-lib-2.2-jdk15.jar
Action:
public String findAll() throws IOException{
List<News> news = newsService.findAll();
//把java 对象列表转换为json对象数组,并转为字符串
JSONArray array = JSONArray.fromObject(news);
//将数据输送到后台,设置数据编码格式和防止后台自动缓存
this.getResponse().setContentType("text/html; charset=utf-8");
this.getResponse().setHeader("Cache-Control", "no-cache");
this.getResponse().getWriter().print(array);
return null;
}
客户端: