关闭→
当前位置:文萃集>赢在职场>电脑培训>jQuery解析json数据实例分析问题

jQuery解析json数据实例分析问题

文萃集 人气:7.07K

先来看看我们的Json数据格式

jQuery解析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 { haracterEncoding("UTF-8"); haracterEncoding("UTF-8"); ontentType("text/html;charset=UTF-8"); lter(req, resp);}

服务端我用Servlet生成json数据(代码片段)。

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = riter(); //过滤器已经做过编码转化了。 ontentType("text/html;charset=UTF-8"); StringBuffer sb = new StringBuffer(); nd("[{id:01,name:"小白",old:29,sex:"男"},"); nd("{id:02,name:"小蓝",old:29,sex:"男"},"); nd("{id:03,name:"小雅",old:29,sex:"男"}]"); t(sb);}

页面端JQuery代码:

<%@ page="" contenttype="text/html;charset=UTF-8" language="java">

写到、文件中的json数据,没有被解析出来,Firebug中调试了一下,10行断点下一步直接结束,就没有遍历对象数组。于是分别测试了一下文本文件 sp文件 ervlet 返回的'数据,浏览器只有解析出Servlet的返回的数据是json数据希望本文所述对大家jQuery程序设计有所帮助。

TAG标签:#jQuery #实例 #json #解析 #