简体   繁体   English

解析作为响应返回的JSON对象

[英]parse the JSON object returned as response

$(document).ready(function(){
    $('#id1').click(function(){

        $.ajax({
            type:"GET",
            url:" ggs.erm.servlet.setup5.Page",
            success:function(response){
                var obj = JSON.parse(response);
                alert(obj);
            }
        });
    });
});

I am facing problem with parsing JSON object i receive from server. 我在解析从服务器收到的JSON对象时遇到问题。

Connection con = null;
JSONObject json = new JSONObject();
JSONArray jarray = new JSONArray();
try{
    con =ConnectionPool.getConnection();
    String sql = "select country from country_name";
    Statement stmt = con.createStatement();
    ResultSet rs =  stmt.executeQuery(sql);
    while(rs.next())
    {
        jarray.put(rs.getString(1));    
    }
    json.put("country", jarray);
}
catch(Exception e){e.printStackTrace();}
finally{
    try {
        con.close();
    } catch (SQLException e) {
    e.printStackTrace();}
    }
    response.setContentType("application/json");
    try {
        response.getWriter().write(json.toString());
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
}

This is code for generating json. 这是用于生成json的代码。
Problem is How to parse the JSON object I get on Client Side. 问题是如何解析我在客户端获得的JSON对象。

Don't use alert() to debug. 不要使用alert()进行调试。 It just gives you the toString() value, which is [object Object] . 它只是给您toString()值,即[object Object]

Instead, log the object to the console. 而是,将对象记录到控制台。

console.log(response);

// or

var obj = JSON.parse(response);
console.log(obj);

Open the developer tools in your browser to view the content of the object. 在浏览器中打开开发人员工具以查看对象的内容。

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

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