当前位置:编程学习 > JS >>

json的使用原理及实例

这次在项目中前后台的数据交互中用到了json,经过这段时间的使用,大概了解了一下,简单总结一下json。
JSON:JavaScript 对象表示法(JavaScript Object Notation)。
JSON 是存储和交换文本信息的语法。类似 XML。
JSON 比 XML 更小、更快,更易解析。
和 XML 一样,JSON 也是基于纯文本的数据格式。由于 JSON 天生是为 JavaScript 准备的,因此,JSON 的数据格式非常简单,您可以用 JSON 传输一个简单的 String,Number,Boolean,也可以传输一个数组,或者一个复杂的 Object 对象。
先看controller中的一段代码。看主要是看从数据库查询出来的数据是怎样以json的格式输出的。
[java] 
@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代码:
[java] 
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判断返回的信息。
[javascript] 
<span style="white-space:pre">  </span>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; 
            &nbs

补充:web前端 , JavaScript ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,