如何将JSONArray对象放在servlet中的JSONObject中

时间:2013-07-26 15:37:40

标签: java ajax json servlets

我正在尝试使用$ .ajax()方法动态填充多个selectbox .... 这是我的HTML代码......

    <select id="imageSize" name="ImageSizeId"></select>
    <select id="circulation" name="circulationId"></select>

这是我的servlet代码.....

    protected void doGet(HttpServletRequest request, HttpServletResponse response)   throws ServletException, IOException {
    response.setContentType("text/json;charset=utf-8");
    response.setHeader("Cache-Control", "no-cache");
    PrintWriter out = response.getWriter();
    JSONObject usesfactors = new JSONObject();  
    JSONArray jArraysize = new JSONArray();
    JSONArray jArraycirculation = new JSONArray();
    Statement stmtsize,stmtcirculation;
    ResultSet rssize,rscirculation;
    int category_id = Integer.parseInt(request.getParameter("CategoryId"));
    int format_id = Integer.parseInt(request.getParameter("FormatId"));
    String size,display,des;
    int sizeid;



  try {
          if(category_id == 2 && format_id == 201){

    // Get image size factors

                     String sql1="SELECT SizeId,Size FROM Size WHERE Category_id = "+category_id+" AND Format_id = "+format_id+" ";                                          
                     PreparedStatement ps1 = conn.prepareStatement(sql1);
                     rssize =  ps1.executeQuery() ; 

                    if( rssize!=null){
                               System.out.println("Not Null");// its printing even if resultset rssize has no records.......why?
                            while(rssize.next())
                                {   
                                     System.out.println("inside resultset");            
                                     JSONObject jobj = new JSONObject();  
                                     sizeid = rssize.getInt("SizeId");
                                     size=rssize.getString("Size");  
                                     System.out.println(size);
                                     jobj.put("SizeId", sizeid);
                                     jobj.put("Size", size);                        
                                     jArraysize.add(jobj);
                                }
                              usesfactors.put("Size", jArraysize);
                              rssize.close();
                    }
                    else{
                              System.out.println("Does not have Size factors");
                    }

    // Get image circulation factors  

                    String sql2="SELECT circulationId,circulation FROM Circulation WHERE Category_id = "+category_id+" AND Format_id = "+format_id+" ";
                    PreparedStatement ps2 = conn.prepareStatement(sql2);                      
                    rscirculation =  ps2.executeQuery() ;    

                    if(rscirculation!=null){
                            while(rscirculation.next())
                                {      

                                     JSONObject jobj = new JSONObject();  
                                     display = rscirculation.getString("DisplayName");
                                     des=rscirculation.getString("Description");                                               
                                     jobj.put("DisplayName", display);
                                     jobj.put("Description", des);                        
                                     jArraycirculation.add(jobj);

                                }
                            usesfactors.put("Circulation", jArraycirculation);
                            rscirculation.close();
                  }
                    else{
                              System.out.println("Does not have Circulation factors");
                    }

                  out.println(usesfactors);
          }

我得到空的json结果....?错了? { “大小”:[], “循环”:[]} 我不想执行这个stmt“usesfactors.put(”Size“,jArraysize);”如果resultset rssize为null但是这个stmt正在执行,即使结果集rssize没有记录.... 总之,我想将json数组(jArraysize)放在json对象(usesfactors)中,当且仅当结果集rssize有记录时。

2 个答案:

答案 0 :(得分:0)

The problem in your code might be your sql queries which are not returning any values.because i have done in my IDE in the same way you have done,only difference is that instead of getting values from database i have kept manually by iterating for loop.below is the code...
JSONObject usesfactors = new JSONObject();  
             JSONArray jArraysize = new JSONArray();
             JSONArray jArraycirculation = new JSONArray();

             for(int i=0;i<3;i++)
             {
             JSONObject jobj = new JSONObject();  
             jobj.put("SizeId", "1");
             jobj.put("Size", "2");                        
             jArraysize.put(jobj);                
             }
             usesfactors.put("Size", jArraysize);

             for(int i=0;i<3;i++)
             {
                  JSONObject jobj = new JSONObject();  
                  jobj.put("DisplayName", "3");
                  jobj.put("Description", "4");                        
                  jArraycirculation.put(jobj);
             }
             usesfactors.put("Circulation", jArraycirculation);
             System.out.println(usesfactors);

答案 1 :(得分:0)

在回答代码中有关其进入rssize!=null块的原因的问题时,请参阅javadoc on executeQuery。它明确声明它永远不会返回null,因此null检查总是通过。这意味着空结果集将跳过该部分中的循环,但仍然按

usesfactors.put("Size", jArraysize);

这应该被重写以实际避免在空返回集上插入。应该进行类似的更改以纠正循环部分,这也会进行无效的无效检查。

while(rssize.next())
{   
  System.out.println("inside resultset");            
  JSONObject jobj = new JSONObject();  
  sizeid = rssize.getInt("SizeId");
  size=rssize.getString("Size");  
  System.out.println(size);
  jobj.put("SizeId", sizeid);
  jobj.put("Size", size);                        
  jArraysize.add(jobj);
}
if (jArraysize.length > 0)
{
  usesfactors.put("Size", jArraysize);
}
else
{
  System.out.println("Does not have Size factors");
}
rssize.close();