技术开发 频道

json 的使用

[IT168技术文档]使用后好处 
    java 和 javascript 使用同统一对象,语法 . 数据从 sever 到 v层 和 v层 到 sever  方便.
            简单说就是 JBean 不做修改拿到V层 当 JSBean (javascript 类) 中用

1.url 乱码问题参照 Ajax uri 乱码问题总结(IE,FF)
2.使用jar是 jxpath ; json  ......(与相关)
               jxpath 参照 jxpath 学习笔记 
               json 参照 使用json-lib 

例题说明 数据 name,avg 在通过 ajax json 后 avg +1 再展现到页面
本页需要 json.js 下载到 http://www.json.org/json.js 
<% @ page pageEncoding = " GBK " %> <% @ page contentType = " text/html; charset=GBK " %> < html > < head > < title > json.html </ title > < meta http-equiv ="keywords" content ="keyword1,keyword2,keyword3" > < SCRIPT src ="../js/json.js" ></ SCRIPT > < script language ="javascript" type ="text/javascript" > var request = false ; // javascript Ajax 没什么好说的 这可以用 prototype prototype.js 的理解 ,dojo dojo 等 ajax //本例 为方便直接写了 ^_^ function getOpen(){ try { request = new XMLHttpRequest(); } catch (trymicrosoft) { try { request = new ActiveXObject( " Msxml2.XMLHTTP " ); } catch (othermicrosoft) { try { request = new ActiveXObject( " Microsoft.XMLHTTP " ); } catch (failed) { request = false ; } } } } function getCustomerInfo() { getOpen(); if ( ! request) alert( " Error initializing XMLHttpRequest! " ); //这通过 url 把数据传给 server //数据来源 javascript 类 就下方 DBdata string: name:我名字^_^ , int: avg:年龄^_^ var url = " /json/json?jsonStr= " + ( new DBdata()).toJSONString() ; request.open( " GET " , url, true ); request.send( null ); request.onreadystatechange = updatePage; } // ajax 处理 数据后返回的 结果 // 年龄 +1 并输入到 输入框中 function updatePage(){ if (request.readyState == 4 ) if (request.status == 200 ){ //得到 json str var jss = request.responseText; //加载到 javascript 类中 string -> jsBean var jsobj = eval('(' + jss + ')'); //使用和 java对象一样 ^_^ document.getElementById('xx').value = jsobj.name + ':' + jsobj.avg ; } } //数据来源 function DBdata(){ this .name = '刘凯毅'; this .avg = 23 ; this .init = function (){ alert('呵呵'); } } </ script > </ head > < body onload ="" > < INPUT id ='xx' type ="text" > < INPUT id ='x' type ="button" onclick ="getCustomerInfo()" value ="go" > </ body > </ html >

 

为了方便明了我java方就使用了 server

package servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.jxpath.JXPathContext; import net.sf.json.JSONObject; public class Json extends HttpServlet { public void doGet(HttpServletRequest req,HttpServletResponse rpo)throws ServletException,IOException{ rpo.setCharacterEncoding("GBK"); req.setCharacterEncoding("GBK"); rpo.setContentType("text/html; charset=GBK"); PrintWriter out = rpo.getWriter() ; //得到 url 传入数据 String str = req.getParameter("jsonStr") ; //java 方 string -> javaBean JSONObject jso = JSONObject.fromString(str); //javaBean 使用jxpathcontxt解读更方便 ^_^ //其实jso中是以 map 形式存区的 有兴趣的可以自己动手写下哦 JXPathContext jx = JXPathContext.newContext(jso); try { //jxpath好处来了 avg + 1 jx.setValue("./avg", jx.getValue("./avg + 1 ") ); } catch (Exception e) {e.printStackTrace();} //以 jsonString 传出 out.print(jso.toString()); } }

 

web.xml

<servlet> <servlet-name>json</servlet-name> <servlet-class>servlet.Json</servlet-class> </servlet> <servlet-mapping> <servlet-name>json</servlet-name> <url-pattern>/json</url-pattern> </servlet-mapping>
原文地址
0
相关文章