Q:

Android json error com.google.gson. JsonSyntaxException: java.lang. IllegalStateException

I am facing this error in parsing json data  
Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2

I could'nt find the solution.  My json data is :
{
  "user": [
    {
      "email": "wijden@jerseyrest.com",
      "firstName": "Wijden",
      "id": "1",
      "lastName": "User"
    }, {
      "email": "user@jerseyrest.com",
      "firstName": "Sample",
      "id": "2",
      "lastName": "User"
    }, {
      "email": "ingenieur@jerseyrest.com",
      "firstName": "Ingenieur",
      "id": "3",
      "lastName": "User"
    }
  ]
}

And this is how I did it :          
Type type = new TypeToken<List<WorkItem>>() { }.getType();
List<WorkItem> workitems = (List<WorkItem>) new Gson().fromJson(resultat, type);

I would greatly appreciate it if you can help me solve tnis problem.  Thanks in advance

A:

In order to parse your JSON, I'd create classes to wrap the response, namely:
public class Response {
  @SerializedName("user")
  private List<User> userList;
  //getters and setters
}

and,
public class User{
  @SerializedName("id")
  private int id;
  @SerializedName("email")
  private String email;
  @SerializedName("firstName")
  private String firstName;
  @SerializedName("lastName")
  private String lastName;
  //getters and setters
}

Then, in order to parse your JSON reponse, you just have to do:
Gson gson = new Gson();
Response data = gson.fromJson(yourJsonString, Response.class);

Then you can access your data very easily, like:
User user = data.getUserList.get(i);

Note: The use of the annotation @SerializedName is interesting to separate the name of a field in the JSON response and in your app, in order to follow Java naming conventions...

