gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 05:51:29 24 4
gpt4 key购买 nike

$(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);
}
});
});
});

我在解析从服务器收到的 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();
}
}

这是生成 json 的代码。
问题是如何解析我在客户端获得的 JSON 对象。

最佳答案

不要使用alert() 进行调试。它只是为您提供 toString() 值,即 [object Object]

相反,将对象记录到控制台。

console.log(response);

// or

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

在浏览器中打开开发者工具以查看对象的内容。

关于javascript - 解析作为响应返回的 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14598049/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com