4

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": "[email protected]",
      "firstName": "Wijden",
      "id": "1",
      "lastName": "User"
    }, {
      "email": "[email protected]",
      "firstName": "Sample",
      "id": "2",
      "lastName": "User"
    }, {
      "email": "[email protected]",
      "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

1
  • JSONLint is your friend: jsonlint.com. In this case, the three lines you cut/pasted are valid. So the problem is "something else". Q: Could you provide more code? Q: Could you break things down into a few extra subvariables, instead of doing everything all at once with "new Gson().fromJson()"? Commented Apr 18, 2013 at 19:23

2 Answers 2

2

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...

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for you reply, the annotation @SerializedName is very interesting but I'm using REST Web service with Jersey that used jackon not gson any other solution?
I don't understand your point... My solution doesn't modify anything in the web service! it's just what you have to write in your app to parse the JSON response you're getting... and you are using GSON, because in your code you have new Gson() and you tagged your question with gson...
Really? I've edited the code, because I wrote firstName as an int and it's a String... but I'm running this code with the JSON response you provided and it's working perfectly for me... Are you sure you're getting the same exception? or it may me because of the mistake I'm pointing out in private int firstName?
Sorry I have an other error which is java.lang.ClassCastException: com.client.android.model.WorkItemContainer cannot be cast to java.util.List and the app is displaying a list without the right informations
Where are you using WorkItemContainer class? That's not in my solution... I suggest you to open a new question with your updated code following my approach and all the details the new problem, and I'll try to help...
|
0

When you try to parse a collection like that Gson expects you to have a JSON ARRAY (as the error message states), not an Object. Your JSON is an OBJECT that contains an ARRAY (an array of objects).

To parse that you'll need a Java Object that HAS a collection as your type. Or, you'll need to change your json to be an array, or you might be able to create a custom serializer/deserializer (or come up with some other trick, but the bottom line is that your JSON is not an array, at present).

Try changing your Java object that you parse it into, for a quick fix:

public class WorkItemsContainer {
  private List<WorkItem> workItems;
  //getters/setters/ctor/etc
}

See the Gson docs "with objects" section here: https://sites.google.com/site/gson/gson-user-guide#TOC-Object-Examples.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.