controller
这里我直接在浏览器运行过,直接会生成JSON数据格式
@RequestMapping("/api0")
public String openTestView2(Model model) {
return "test-api/List1";
}
@RequestMapping("/getNCL")
public void getData(HttpServletResponse response,Ncl searchCondition){
int totalCount = nclService.getTotalCount(searchCondition);
List<Ncl> list = nclService.getNclData(searchCondition);
ObjectMapper map = new ObjectMapper();
String rst = "";
try {
rst = map.writeValueAsString(list);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
StringBuilder sb = new StringBuilder();
sb.append("{\"Rows\":");
sb.append(rst);
sb.append(",\"Total\":");
sb.append(totalCount + "}");// 追加所有记录数到json
rst = sb.toString();
JsonUtil.writeJson(response, rst);// 向页面输出数据
}
返回的json
{"Rows":[{"name":null,"year":2014,"mouth":1},{"name":null,"year":2014,"mouth":1}],"Total":28}
jsp
$(document).ready(function() {
$.ajax({
//请求方式为get
type:"GET",
//json文件位置
url:"${ctx}/api/getNCL.do",
dataType: "text",
contentType: "application/json; charset=utf-8",
success: function(data){
//json问题所以要把null弄成"null"
var datanew = data.replace(/null/g,"\"null\"");
$("div").append(datanew);
}
})
});
这样运行是可以在页面上加载出json格式数据的。但是如果调用datanew.rows[n]就不行了,解析不出数据来!
求各位大神帮忙看一下是怎么回事!!!万分感谢!!!