时间:2021-07-01 10:21:17 帮助过:5人阅读
第一个案例:jquery 遍历json对象集合 常用示例
jsp中
jsonText的格式:
{"status":"200","msg":[{"id":"1","name":"n1"},{"id":"2","name":"n2"}]}
{"status":"500","msg":"异常信息"}
java中:
List pngFileList = new ArrayList();//某对象集合
if(null != pngFileList && pngFileList.size() > 0) {
JSONArray pngFileArray =JSONArray.fromObject(pngFileList);
}
if(null != pngFileArray){
this.setTextAjax(pngFileArray.toString());
//异常的格式
//this.setTextAjax("{\"status\":\"500\",\"msg\":\""+errormsg+"\"}");//没有记录
/**
* ajax返回html,包括json形式
*
* @param responseContent
*/
public void setTextAjax(String responseContent) {
try {
HttpServletResponse response = getHttpResponse();
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
response.setHeader("Pragma", "No-cache");
response.setHeader("Content-Type", "text/html");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
PrintWriter out = response.getWriter();
out.print(responseContent);
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
// ajaxResponse = new StringBufferInputStream(responseContent);
}
第二个案例:jQuery 遍历JSON 对象
不说别的,直接贴代码:
第三个案例:jquery中遍历读取json串中的对象
假设我们从服务器端获取如下的json串,其中包括数组。我们该如何遍历读取呢?
使用.each难度太高,直接js读取吧
通过如上的方式就能获取json串的json对象并进行输出。
为大家分享这么多的案例,就是想帮助大家掌握jquery遍历json对象集合的方法,真正理解,希望这篇文章可以帮助到大家。