struts2查询的json格式的数据返回结果错误
action中:
public String findAllUserJson(){
HttpServletRequest req = ServletActionContext.getRequest();
List<Map<String, Object>> rows=new ArrayList<Map<String,Object>>();
rows=userService.selectUser();
Map<String, Object> obj = new HashMap<String, Object>();
obj.put("total", rows.size());
obj.put("rows", rows);
result=JSONObject.fromObject(obj);
System.out.println("resultresult="+result);
//List<User> users = userService.selectUserByCondition(new User());
return "userListJson";
}
service中:
public List<Map<String, Object>> selectUser() {
return userDao.selectUser();
}
dao中:
public List<Map<String, Object>> selectUser() {
return this.getSqlSession().selectList("cn.itcast.mybatis.domain.User.selectUserByConditionForMap");
}
mybatis中:
<!-- 动态sql -->
<select id="selectUserByConditionForMap" resultType="hashmap">
select * from user
</select>
查询结果为:
{"total":2,"rows":[{"AGE":10,"age":10,"address":"address001","NAME":"zhangsan","name":"zhangsan","id":"001","ADDRESS":"address001","ID":"001"},{"AGE":20,"age":20,"address":"address002","NAME":"wangwu","name":"wangwu","id":"002","ADDRESS":"address002","ID":"002"}]}
问题:
数据中只有age,id,address,name字段的两行数据,现在查询结果怎么有重复的数据id,ID,ADDREDD,address等
补充:struts配置文件中:
<result name="userListJson" type="json">
<param name="root">result</param>
</result>
------解决方案--------------------row 打印一下,看看啥情况
------解决方案--------------------那个 json数据有struts 那个result 默认的name值是“SUCCESS”,并且这个值没有人改,,,这个值你不应该自定义。。。
我好像以前也遇到过这个问题。。。 你再前台调试一下,看前台得到响应是什么?
------解决方案--------------------我建议还是不要 在后台拼接json数据,我建议用一个对象将这些数据封装,然后,把这个对象放在Collection接口中,直接通过
ActionContext.getContext().put("lists",collectionlist);//将数据放进对象栈,然后,通过Ajax直接获取,具体是
$.post("xxxAction?......",null,function(data){
alert(data.lists);//这里就获取到了然后可以进行遍历。。。
});