在jQuery完成后检索JSON对象

时间:2013-10-08 03:33:14

标签: java javascript jquery json response

我正在尝试使用jQuery get检索JSON对象,而我要检索的Object要嵌入innerHTML中。以下代码是我如何构建我的JSON

getListOfActivity.jsp

<%  
        String urusStr = request.getParameter("ukid");
        int urusId = Integer.parseInt(urusStr);

        lkpPdkCommon[] activity = getListOfActivity(urusId);
        if(activity!=null){
            out.println("{PartList:");
                out.println("[");
                    for(int x=0;x<2;x++){// the lkpPdkCommon[] return from getListOfActivity(urusId) huge so I limit the array to 2         
                        out.println("{");
                            out.println("ActivityID:\""+activity[x].getID()+"\",Description:\""+activity[x].getDescription()+"\"");
                        out.println("}");
                        if((x+1)!=2){
                            out.println(",");
                        }
                    }
                out.println("]");
            out.println("}");   

            response.setContentType("application/json");

    %>

以下代码是我的jQuery / jscript

    var ukid = document.getElementById("ukid").value
    var aktivityId = row.insertCell(1);
    var description = row.insertCell(2);
    var JSONObject;         
    var $ac = jQuery.noConflict();
    $ac.get("../../getListOfActivity.jsp",{ukid:ukid}, function(data){  
        JSONObject = data   

//for testing purposes I do not iterate through the JSON Object

        aktivityId.innerHTML = JSONObject.PartList[0].ActivityID
        description.innerHTML = JSONObject.PartList[0].Description
    });

以下代码未返回任何错误,但似乎无效。这是我使用firebug检查的JSON对象

JSON Object

2 个答案:

答案 0 :(得分:0)

您是否尝试过Jquery getJSON。

我认为这会对你有帮助。

http://api.jquery.com/jQuery.getJSON/

答案 1 :(得分:0)

我在jsp

中修改了以下行后问题解决了
out.println("{PartList:");

out.println("{\"PartList\":");

    out.println("ActivityID:\""+activity[x].getID()+
"\",Description:\""+activity[x].getDescription()+"\"");

    out.println("\"ActivityID\":\""+activity[x].getID()+
"\",\"Description\":\""+activity[x].getDescription()+"\"");

在进行修改之前,响应头发送的原始JSON对象如下所示:

{PartList:
        [
            {ActivityID:"8638",Description:"GERMS"},
            {ActivityID:"8639",Description:"GOVERNMENT CERTIFY PROGRAMMES"}
        ]
}

并且在修改之后

{"PartList":
        [
             {"ActivityID":"8638","Description":"GERMS"},
             {"ActivityID":"8639","Description":"GOVERNMENT CERTIFY PROGRAMMES"}
        ]
}