json的使用原理及实例
来源:互联网 发布:php json encode 乱码 编辑:程序博客网 时间:2024/06/05 02:06
这次在项目中前后台的数据交互中用到了json,经过这段时间的使用,大概了解了一下,简单总结一下json。
JSON:JavaScript 对象表示法(JavaScript Object Notation)。
JSON 是存储和交换文本信息的语法。类似 XML。
JSON 比 XML 更小、更快,更易解析。
和 XML一样,JSON 也是基于纯文本的数据格式。由于 JSON 天生是为 JavaScript 准备的,因此,JSON 的数据格式非常简单,您可以用 JSON 传输一个简单的 String,Number,Boolean,也可以传输一个数组,或者一个复杂的 Object 对象。
先看controller中的一段代码。看主要是看从数据库查询出来的数据是怎样以json的格式输出的。
@RequestMapping("/work/plan/checkSubmitForApproval")public void checkSubmitForApproval(String planId,HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{String result="{\"result\":\"faild\",\"personSituation\":\"null\"}";HttpSession session = request.getSession();String industryID = (String) session.getAttribute("industryID"); IIndustry industry = industryService.getById(industryID); if(industry.getType().equals("XXX")){ try { boolean flag = false; IProjectMain yearPlan = projectPlanService.findProjectPlanById(planId); List<IStaffInfo> listStaffInfo = sysStaffService.getStaffByPlanId(planId, industryID); for(int i=0;i<listStaffInfo.size();i++){ if(listStaffInfo.get(i).getPractitionersPost().equals(StaffRole.PROGECTMANAGER.toString())){ flag = true; } } if(flag == true){ result="{\"result\":\"success\",\"personSituation\":\""+yearPlan.getPerson_Situation()+"\"}"; }else{ result="{\"result\":\"success\",\"personSituation\":\""+yearPlan.getPerson_Situation()+"\",\"isManager\":\"false\"}"; } } catch (Exception e) { result="{\"result\":\"falid\"}"; throw new PlatformException(e); }finally{ OutputUtils.write(response,result,"text/x-json;charset=UTF-8"); }
先PutputUtils中的write代码:
public static void write(HttpServletResponse response, String text, String contentType) { PrintWriter out=null; response.setHeader("Pragma", "No-cache");response.setHeader("Cache-Control", "no-cache");response.setDateHeader("Expires", 0); response.setContentType(contentType); try { out = response.getWriter();out.write(text); } catch (IOException e) { Logger.getLogger(OutputUtils.class).error(e.getMessage(), e); } finally{if(out!=null){out.flush();out.close();}} }
其中的思路是得到response的printwriter,将要输出的信息设置到其中。在界面层利用jquery的Post判断返回的信息。
function distribute(){var dplanId = $(".currli").attr("id");if(dplanId != ""){ $.ajax({ type : "POST", url :做验证的action url, dataType : "json", success : function(data) { //HAVE为已分配状态 if (data.result == "success" && data.personSituation == "UNHAVE") { with (document.getElementById("planForm")) { action=验证合法后要提交的url; method="post"; submit(); }}其中success:function(data)是一个回调函数,即上面做的验证action的方法成功之后执行的操作。在jquery的使用方法详情点击这里查看。
关于jquery的post提交不理解的同学,点击这里学习。
关于ajax和jquery的历史,建议参见维基百科中,写的很清楚。
jquery已经封装好了从response中取data的操作,所以这里用起来非常方便,省去了从xml中一点一点读取的头疼,给开发带来了极大方便。
- json的使用原理及实例
- ZooKeeper原理及使用实例
- css sprite的原理、优缺点及使用实例
- Solr的原理及在项目中的使用实例.
- json实例及各种形态的转换
- BlazeDS的功能原理及配置实例
- 霍夫变换的原理及实例
- 链表逆序的原理及实例
- WebService的工作原理及实例
- jquery下json的使用实例
- java中AJAX使用JSON的实例
- 触发器原理及实例
- Ajax实例及原理
- ContentProvider原理及实例
- 多线程原理及实例
- ContentProvider原理及实例 .
- ContentProvider原理及实例
- ContentProvider原理及实例
- [Erlang 0079] RabbitMQ 初探
- jetspeed的简单描述
- 类执行机制
- regular pad(正规焊盘),thermal pad(热风焊盘) 和anti pad(隔离盘)的区别使用
- 牢骚,真的有点牢骚
- json的使用原理及实例
- PCA降维算法总结以及matlab实现PCA(个人的一点理解)
- redhat linux 安装总结
- pdm中的表 在xml文件中的结构展现
- linux下查找文件内容!find+grep
- 黑马程序员_代码块
- 分析SurfaceFlinger过程遇到的知识点
- 修改ssh 非root登录
- dwr的Reverse Ajax