------第一种方式:未extends="json-default"-------------
-------------------------action类-----------------------------------
public void getJson(){
try {HttpServletRequest request=ServletActionContext.getRequest();
HttpServletResponse response=ServletActionContext.getResponse();
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
List list=new ArrayList();
list.add("ly");
list.add("ly1");
list.add("ly2");
list.add("ly3");
list.add("ly4");
list.add("ly5");
PrintWriter out=response.getWriter();
JSONArray JList=JSONArray.fromObject(list);
String jsonS=JList.toString();
out.write(jsonS);
out.flush();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
----------------------------------配置文件-------------------------------------
<action name="testJsonAction_getJson" class="cn.gdpe.controller.TestJsonAction" method="getJson"></action>
---------------第二种方式:使用内置的json extends=“json-defalut”
我的其他博客 已写