自己对前台解析Json、后台生成Json的理解和测试
来源:互联网 发布:数位板绘画软件 编辑:程序博客网 时间:2024/04/30 23:41
这次练习只用到了前台JSP页面和后台Controller里方法,不走数据库。在controller里模拟数据库。
首先建了一个student实体类。然后在controller里创建3个对象。
student sd1 = new student(1,"bob",23); student sd2 = new student(2,"tom",25); student sd3 = new student(3,"jax",27);
然后创建JSONArray对象,直接将实体对象放进去。
JSONArray json = new JSONArray(); json.add(sd1); json.add(sd2); json.add(sd3);
或者创建JSONOjbject对象,也可以;
JSONObject json = new JSONObject(); json.put("s1",sd1); json.put("s2",sd2); json.put("s3",sd3);
要么就是像Mybatis一样,直接返回个list集合,然后将集合装进JSONArray里,这种貌似最经典,常用。注意静态方法!!!
List<student> list = new ArrayList<student>(); list.add(sd1);
list.add(sd2);
list.add(sd3);
JSONArray json = JSONArray.fromObject(list);
Map<String,student> map =new HashMap<String,student>(); map.put("sd1",sd1); map.put("sd2",sd2); map.put("sd3",sd3);JSONObject json = JSONObject.fromObject(map)
基本上就这些方式,下面就是创建流来响应到前台。
PrintWriter pw = null; try { pw = response.getWriter(); pw.write(json.toString()); }catch (IOException e){ e.printStackTrace(); }finally{ if(pw != null){ pw.close(); } }
好了,controller里就完事了。下面就是JSP里处理JSON了。
首先是ajax部分
$(function(){ $.ajax({ url: "/json1.action", dataType : "json", type : "post", success : function(result,status,xhr){ console.log(result); console.log(status); console.log(xhr); /* console.log(result[0].sname);*/ /*var data = JSON.parse(result);*/ /* var data = eval('('+result+')');*/ addhtml(result); }, error : function(xhr,status,error){ alert("ajax异常"); console.log(error); console.log(status) console.log(xhr); } }); });
然后调用addhtml()方法。
function addhtml(data){ var t_body = ""; /*遍历json数组*/ /*for(var i = 0;i<data.length;i++){ t_body += "<tr><td>"+data[i].sid+"</td>"+"<td>"+data[i].sname+"</td>"+"<td>"+data[i].sage+"</td></tr>"; }*/ /*遍历json对象*/ for(var i in data){ t_body += "<tr><td>"+data[i].sid+"</td>"+"<td>"+data[i].sname+"</td>"+"<td>"+data[i].sage+"</td></tr>"; } $("tbody").html(t_body); }
上边有两种遍历方式,第一种偏向与后台是JSONArray的json,for( var i in data)这种没有任何限制,也不出错。用html拼接来加入到tbody里。
<table border="1"> <thead> <td>编号</td> <td>姓名</td> <td>年龄</td> </thead> <tbody></tbody> </table>
基本上就是这样。
页面就是
好了,折腾到最后,才知道,json有两种,一种是对象,一种就是字符串。 上面进行解析的只能是json对象。如果不是对象,用JSON.parse().来转换。
阅读全文
0 0
- 自己对前台解析Json、后台生成Json的理解和测试
- 后台和前台对json的处理
- javascript前台生成json,后台进行解析
- php对json字符串的解析以及 php后台和前台js的交互
- java后台JSON解析前台传入的数组JSON字符串
- ajax自己要注意的和后台传Json到前台的方式
- JSP前台获取使用后台生成的json
- 后台对前台传入的json数据进行数据类型判断
- JAVA后台接收前台传过来的json字符串并解析获得key 和value
- 前台将多个json对象传到后台解析
- 前台json数组传到后台解析
- Json:前台对象数组传到后台解析
- Json:前台对象数组传到后台解析
- Json:前台对象数组传到后台解析
- Json:前台对象数组传到后台解析
- Json的解析和生成
- Json的解析和生成
- json的生成和解析
- HLS m3u8文件更新规则
- 新浪授权登录 正确的获取access_token (都不能用curl请求,包括微信授权登录 ) 要用file_get_contents
- native和static native区别
- [转]OkHttp3 最有营养的初级教程
- [置顶]各种标志位的含义
- 自己对前台解析Json、后台生成Json的理解和测试
- [置顶]基于51单片机+DAC0832的信号发生器
- Python安装包常见错误
- [置顶]Python基本语法初试
- Python中读取txt文本出现“ 'gbk' codec can't decode byte 0xbf in position 2: illegal multibyte sequence”的解决办法
- [置顶]树莓派B+使用心得
- hdu 4578 Transformation(线段树+多种操作)
- [置顶]SFTP服务的使用!!
- [置顶]eNSP仿真学习,网络入门!