user1909647
user1909647

Reputation:

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.

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.
Problem is How to parse the JSON object I get on Client Side.

Upvotes: 2

Views: 2845

Answers (1)

the system
the system

Reputation: 9336

Don't use alert() to debug. It just gives you the toString() value, which is [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.

Upvotes: 1

Related Questions