- 相關推薦
jQuery解析json數據實例分析問題
先來看看我們的Json數據格式:
[{id:01,name:"小白",old:29,sex:"男"},{id:02,name:"小藍",old:29,sex:"男"},{id:03,name:"小雅",old:29,sex:"男"}]
為了消除亂碼問題,我們設置一個過濾器(代碼片段)
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);}
服務端我用Servlet生成json數據(代碼片段)。
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); //過濾器已經做過編碼轉化了。 resp.setContentType("text/html;charset=UTF-8"); StringBuffer sb = new StringBuffer(); sb.append("[{id:01,name:"小白",old:29,sex:"男"},"); sb.append("{id:02,name:"小藍",old:29,sex:"男"},"); sb.append("{id:03,name:"小雅",old:29,sex:"男"}]"); out.print(sb);}
頁面端JQuery代碼:
<%@ page="" contenttype="text/html;charset=UTF-8" language="java">
寫到.jsp、.txt文件中的json數據,沒有被解析出來,Firebug中調試了一下,10行斷點下一步直接結束,就沒有遍歷對象數組。于是分別測試了一下文本文件 json.txtjsp文件 json.jspServlet json.do返回的數據,瀏覽器只有解析出Servlet的返回的數據是json數據希望本文所述對大家jQuery程序設計有所幫助。
【jQuery解析json數據實例分析問題】相關文章:
原生ajax調用數據實例簡單講解07-19
硬盤的分區結構及其數據儲存解析07-16
2024小升初數學濃度問題實例解答06-07
團隊中的沖突問題分析02-06
科目三考試中常見問題解析02-03
網絡安全存在的問題及對策分析04-07
國有企業績效管理中存在的問題分析04-15
SATA硬盤數據線導致電腦死機的故障分析04-12