簡體   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);
            }
        });
    });
});

我在解析從服務器收到的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);

在瀏覽器中打開開發人員工具以查看對象的內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM