67,513
社区成员
发帖
与我相关
我的任务
分享
function showResponse(message) {
message = eval("("+message+")");
//处理数据
}
return success("保存成功",JSONArray.fromObject(集合));
protected String success(String message,Object data) {
JSONObject json=new JSONObject();
json.put("status", status);
json.put("data", data);
return ajaxJsonObject(json);
}
protected String ajaxJsonObject(JSONObject json) {
return ajax(json, "text/html");
}
protected String ajax(Object content, String type) {
HttpServletResponse response=null;
try {
response=getResponse();
response.setContentType(type + ";charset=UTF-8");
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.getWriter().print(content);
} catch (IOException e) {
LOG.error(e.getMessage());
}
finally{
try {
response.getWriter().flush();
response.getWriter().close();
} catch (Exception e) {
LOG.error(e.getMessage());
}
}
return null;
}
$.ajax({
url:"personAction_getPerson",
type:"get",
dataType:"json",
success:function(data){
alert(data); //看看有没有值
}
});
List list= new ArrayList();
Person p1 = new Person("张三","123");
Person p2 = new Person("lisi","123");
Person p3 = new Person("王五","123");
list.add(p1);
list.add(p2);
list.add(p3);
System.out.println(list);
String json = JSONArray.fromObject(list).toString();
说的很明确啊 用JSONArraynet.sf.json.JSONException: 'object' is an array. Use JSONArray instead
我把所有代码都发出来,大佬们看一下
前台jsp======
<script type="text/javascript">
$(document).ready(function(){
$.ajax({
url:"personAction_getPerson",
type:"get",
dataType:"json",
success:function(data){
$("#show").empty();
$.each(data,function(i,item){
$("#show").append("<tr><td>"+item.id+"</td><td>"+item.name+"</td><td>"+item.sex+"</td><td>"+item.age+"</td><td>"+item.address+"</td></tr>");
});
}
});
});
</script>
action:======
public String getPerson(){
List<Person> list=ps.getPerson();
JSONObject json = JSONObject.fromObject(list);
result = json.toString();
try {
response.getWriter().write(result);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return SUCCESS;
}
struts.xml
<package name="person" extends="struts-default,json-default">
<action name="personAction_*" class="com.action.PersonAction" method="{1}">
<result name="s">/success.jsp</result>
<result type="json">
<param name="list">result</param>
</result>
</action>
</package>
debug List是有值的