title: How to unmarshall JAX-RS Webservice response (JSON Array) correctly?
body: <p>I have some problem using Jersey REST webservices.</p>
  
  <p>I have the following domain class:</p>
  
  <pre><code>@XmlRootElement
  public class User {
  
      private long id;
      private String email;
      private String forename;
      private String role;
  
      public User() {
          // required for JAXB
      }
  
      // + Getters + Setters
  }
  </code></pre>
  
  <p>and the following resource class:</p>
  
  <pre><code>@Produces(MediaType.APPLICATION_JSON)
  @Path("/users")
  public class UserService {
  
      @GET
      public User[] getUsers() {
          User[] users = ... // Users from DB 
          return users;
      }
  }
  </code></pre>
  
  <p>If I request that resource (/users) I get the following json response:</p>
  
  <pre><code>{
   "user":  
   [
    {
     "id":"1",
     "email":"Toby@email.com",
     "forename":"Toby",
     "role":"admin"
    }
   ,
    {
     "id":"2",
     "email":"Rob@email.com",
     "forename":"Rob",
     "role":"developer"
    }
   ]
  }
  </code></pre>
  
  <p>On the client side (where I have the same User.class) I want to unmarshall this JSON response back to the corresponding POJOs. That means in this example I would like to get two Object of type User. I did some experimental stuff with GSON like</p>
  
  <pre><code>User[] users = gson.fromJson(JSONString, User[].class)
  </code></pre>
  
  <p>but was not able to get it working. Exception:</p>
  
  <pre><code>JSONParseException: Expecting object but found array
  </code></pre>
  
  <p>Can anyone tell me what Iam doing wrong here? Or is my json format the problem?</p>
  
  <p><strong>Edit:</strong> 
  Tried it with an own implementation of a collection type adapter as mentioned <a href="https://stackoverflow.com/questions/6650951/android-json-gson-prom">here</a>:  </p>
  
  <pre><code>Type usersType = new TypeToken&lt;List&lt;User&gt;&gt;() {}.getType();
  List&lt;User&gt; users = new ArrayList&lt;User&gt;();
  users = gson.fromJson(reader, usersType);
  </code></pre>
  
  <p>Now Iam getting an JsonParseException: </p>
  
  <pre><code>The JsonDeserializer failed to deserialize Json Object {"user":[{"id":"1","email":"Toby@email.com","forename":"Toby",....
  </code></pre>