title: How to put JSONArray of object in JSONObject inside servlet
body: <p>I am trying to populate multiple selectbox dynamically using $.ajax() method....
  Here is my html code......</p>
  
  <pre><code>    &lt;select id="imageSize" name="ImageSizeId"&gt;&lt;/select&gt;
      &lt;select id="circulation" name="circulationId"&gt;&lt;/select&gt;
  </code></pre>
  
  <p>Here is my servlet code.....</p>
  
  <pre><code>    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 &amp;&amp; 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);
            }
  </code></pre>
  
  <p>i am getting empty json result....?Whts wrong?
  {"Size":[],"Circulation":[]}
  i don't want to execute this stmt "usesfactors.put("Size", jArraysize);" if resultset rssize is null but this stmt is executing even if result set rssize has no records....
  In short i want to put json array(jArraysize) in json object (usesfactors) if and only if result set rssize has records. </p>