$(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.
JSON.parseas this should be done automatically if the content-type is application/json.alert(response)? I would expect your returned object to have a property calledcountrywhich is an array of string:obj.country[].