title: Serializing List in Java to send as JSON
body: <p>I am trying to serlialize my array in order for it to work even if it contains one element, this is what I did:</p>
  
  <pre><code>@GET
  @XmlElement(name = "contentbean")
  @Path("/retrieveContent")
  @Produces(MediaType.APPLICATION_JSON)
  public JSONObject retrieve(@QueryParam("username") String Username, @QueryParam("path") String Path) throws JSONException {
      ContentBean oContentBean = buildResult(Username, Path);
      List&lt;ContentBean&gt; lContentBeans = new ArrayList&lt;ContentBean&gt;();
      lContentBeans.add(oContentBean);
  
  
      JSONObject jsonObject = new JSONObject();
      jsonObject.append("lContentBeans", lContentBeans);
  
      return jsonObject;
  }
  </code></pre>
  
  <p>But on the client side I receive the following:</p>
  
  <pre><code>["[org.qcri.crosscloud.utils.ContentBean@7f29b922]"]
  </code></pre>
  
  <p>Which is basically an array of my Objects but every object becomes a string in the result. I need to get this:</p>
  
  <pre><code>{"ContentBean":[{"prop1":"val", "prop2":"val"}, {"prop1":"val", "prop2":"val"} ]}
  </code></pre>
  
  <p>Any idea?</p>
  
  <p>Thanks,</p>
  
  <p><strong>UPDATE 1</strong></p>
  
  <p>Adopted J.Lucky's answer but now getting Exception as follows:</p>
  
  <pre><code>org.codehaus.jackson.map.JsonMappingException: No serializer found for class org.qcri.crosscloud.utils.ACLBean and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: java.util.ArrayList[0]-&gt;org.qcri.crosscloud.utils.ContentBean["acl"])
  </code></pre>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>Here is my ContentBean</p>
  
  <pre><code>public static ContentBean buildResult(String sUsername, String sPath){
  
          //Scenario of retrieving an element that is a Directory (so no ACL)
          AttributesBean aB = new AttributesBean();
          aB.setName("profile/"); //From request
          aB.setType(false);  //Hard coded for testing purposes
          aB.setSize(0.2);   //Hard coded for testing purposes
          aB.setLastModified(new Date());
  
          ACLBean aclB = new ACLBean();
          RDFBean rdfB = new RDFBean();
  
          ContentBean cB = new ContentBean();
          cB.setUsername(sUsername); //From request
          cB.setAttributes(aB);
          cB.setACL(aclB);
          cB.setRDF(rdfB);
  
          return cB;
      }
  </code></pre>
  
  <p>ContentBean class:</p>
  
  <pre><code>@XmlRootElement 
  public class ContentBean {
  
      private String username;
      private AttributesBean attributes;
      private RDFBean rdf;
      private ACLBean acl;
  
      /**
       * @return the attributes
       */
      public AttributesBean getAttributes() {
          return attributes;
      }
  
      /**
       * @param attributes the attributes to set
       */
      public void setAttributes(AttributesBean attributes) {
          this.attributes = attributes;
      }
  
      /**
       * @return the rdf
       */
      public RDFBean getRDF() {
          return rdf;
      }
  
      /**
       * @param rdf the rdf to set
       */
      public void setRDF(RDFBean rdf) {
          this.rdf = rdf;
      }
  
      /**
       * @return the acl
       */
      public ACLBean getACL() {
          return acl;
      }
  
      /**
       * @param acl the acl to set
       */
      public void setACL(ACLBean acl) {
          this.acl = acl;
      }
  
      /**
       * @return the username
       */
      public String getUsername() {
          return username;
      }
  
      /**
       * @param username the username to set
       */
      public void setUsername(String username) {
          this.username = username;
      }
  
  }
  </code></pre>
  
  <p><strong>UPDATE 3</strong></p>
  
  <p>Here is the client side:</p>
  
  <pre><code>function loadContent(sUsername, sPath)
          {
              arrayContentBeans = new Array();
              var sUrl = "http://localhost:8080/crosscloudservice/services/RDF/retrieveContent?username="+sUsername+"&amp;path="+sPath;
              $.ajax({
                      type: "GET",
                      url: sUrl,
                      contentType: "application/json",
                      dataType: "json",
                      async: false,
                      success: function parse(resp, status, xhr) {
                         $("#message").html("STATUS: " + xhr.status + " " + xhr.statusText + "\n" + resp);
                         $("#message").hide();
                         $.each(resp, function() {
                              $.each(this, function(i, cb) {
                                  arrayContentBeans.push(cb);
                              });
                         });
  
                         renderContent();
                      },
                      error: function(resp, status, xhr){
                          $("#message").html("ERROR: " + resp.status + " " + resp.statusText + "\n" + xhr);
                          $("#message").show();
                      }
  
                  });
  
          }
  </code></pre>
  
  <p><strong>UPDATE 4</strong></p>
  
  <pre><code>[ {
    "username" : "mzereba",
    "attributes" : {
      "name" : "profile/",
      "size" : 0.2,
      "type" : false,
      "lastModified" : 1410947312123
    },
    "rdf" : {
      "text" : null
    },
    "acl" : {
    }
  } ]
  </code></pre>