- 相關(guān)推薦
jQuery解析json數(shù)據(jù)實(shí)例分析問(wèn)題
先來(lái)看看我們的Json數(shù)據(jù)格式:
[{id:01,name:"小白",old:29,sex:"男"},{id:02,name:"小藍(lán)",old:29,sex:"男"},{id:03,name:"小雅",old:29,sex:"男"}]
為了消除亂碼問(wèn)題,我們?cè)O(shè)置一個(gè)過(guò)濾器(代碼片段)
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException { req.setCharacterEncoding("UTF-8"); resp.setCharacterEncoding("UTF-8"); resp.setContentType("text/html;charset=UTF-8"); chain.doFilter(req, resp);}
服務(wù)端我用Servlet生成json數(shù)據(jù)(代碼片段)。
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); //過(guò)濾器已經(jīng)做過(guò)編碼轉(zhuǎn)化了。 resp.setContentType("text/html;charset=UTF-8"); StringBuffer sb = new StringBuffer(); sb.append("[{id:01,name:"小白",old:29,sex:"男"},"); sb.append("{id:02,name:"小藍(lán)",old:29,sex:"男"},"); sb.append("{id:03,name:"小雅",old:29,sex:"男"}]"); out.print(sb);}
頁(yè)面端JQuery代碼:
<%@ page="" contenttype="text/html;charset=UTF-8" language="java">
寫(xiě)到.jsp、.txt文件中的json數(shù)據(jù),沒(méi)有被解析出來(lái),F(xiàn)irebug中調(diào)試了一下,10行斷點(diǎn)下一步直接結(jié)束,就沒(méi)有遍歷對(duì)象數(shù)組。于是分別測(cè)試了一下文本文件 json.txtjsp文件 json.jspServlet json.do返回的數(shù)據(jù),瀏覽器只有解析出Servlet的返回的數(shù)據(jù)是json數(shù)據(jù)希望本文所述對(duì)大家jQuery程序設(shè)計(jì)有所幫助。
【jQuery解析json數(shù)據(jù)實(shí)例分析問(wèn)題】相關(guān)文章:
原生ajax調(diào)用數(shù)據(jù)實(shí)例簡(jiǎn)單講解07-19
硬盤(pán)的分區(qū)結(jié)構(gòu)及其數(shù)據(jù)儲(chǔ)存解析07-16
2024小升初數(shù)學(xué)濃度問(wèn)題實(shí)例解答06-07
團(tuán)隊(duì)中的沖突問(wèn)題分析02-06
網(wǎng)絡(luò)安全存在的問(wèn)題及對(duì)策分析04-07
關(guān)于公共管理碩士MPA申請(qǐng)與就業(yè)問(wèn)題的解析04-19