简体   繁体   English

javascript 中的 JSON 字符串解析?

[英]JSON String parsing in javascript?

I'm using ajax to make a request on a servletand receive json String我正在使用 ajax 在 servlet 上发出请求并接收 json 字符串

servlet code //Server Side servlet代码 //服务器端

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setHeader("Content-Type", "text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        Gson gson = new Gson();
        try { 
        ArrayList<SearchedCourse> searchedCourses = null;
        int semesterNo = request.getParameter("semesterNo");
        searchedCourses = //Using function to retrieve data from DB and return an ArrayList 
        String courses = gson.toJson(searchedCourses);
        out.write(courses);
                }
            }
        }  finally {
            out.close();
        }
    }

and this is the return json string "I used firebug to copy it"这是返回 json 字符串“我用萤火虫复制它”

[{"courseNumber":1619,"courseNo":"HADTA1100","courseName":"قرآن كريم (1) جزء عم","courseExamDate":"Aug 21, 2011","courseExamTimeFrom":"14:30","courseExamTimeTo":"15:30"},{"courseNumber":1663,"courseNo":"HADTB1100","courseName":"قرآن كريم (2) جزء تبارك","courseExamDate":"Aug 23, 2011","courseExamTimeFrom":"14:30","courseExamTimeTo":"15:30"}]

I use the code below to parse the Json string at the client side我使用下面的代码在客户端解析 Json 字符串

  jQuery.ajax({
    url: 'auth/json/AvailableCoursesGetter',
    type: "GET" ,
    dataType: "json",
    data: {
        'semesterNo': 20112
    },
    success: function(data) {
        for (var i = 0, len = data.length; i < len; i++) {
             document.write(data[i].courseNumber);
        }

    }
});

it works fine on Mozilla FireFox4, but on Google Chrome and IE it doesn't display any thing它在 Mozilla FireFox4 上运行良好,但在 Google Chrome 和 IE 上它不显示任何东西

any help?有什么帮助吗?

@Eli is correct about using the for in being a bad idea, but it seems to me that the code should be working. @Eli 关于使用for in是一个坏主意是正确的,但在我看来,代码应该可以工作。 The following jsFiddle works in IE9: http://jsfiddle.net/shaneblake/arhB7/以下 jsFiddle 在 IE9 中工作: http://jsfiddle.net/shaneblake/arhB7/

Are you sure the data is comming back in the correct format and not as a string?您确定数据以正确的格式返回而不是字符串吗? have you tried using JSON.parse(data) to make sure?您是否尝试过使用JSON.parse(data)来确保?

I just saw that you output the following header in your servlet:我刚刚在您的 servlet 中看到您的 output 以下 header :

response.setHeader("Content-Type", "text/html;charset=UTF-8");

Try changing the content type to application/json .尝试将内容类型更改为application/json

It is considered bad practice to iterate over an array using the for in syntax.使用for in语法遍历数组被认为是不好的做法。 Try this instead:试试这个:

for (var i = 0, len = data.length; i < len; i++) {
    console.log(data[i].courseNumber);
}

If your page in any way has extended the Array prototype, then using for in will cause your script to iterate over those extended properties, and if those are being hit before your actual values, it will most likely cause an error.如果您的页面以任何方式扩展了 Array 原型,那么使用for in将导致您的脚本遍历这些扩展属性,如果这些属性在您的实际值之前被命中,则很可能会导致错误。 The best way to avoid this is to use a traditional for loop.避免这种情况的最好方法是使用传统的 for 循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM