0

I'm having some problems with a conversion of a Json object list to a Java List. I'm using the GSon to do it. I have the following Json list:

{"player":
    [
    {"id":"1","age":"25","name":"Neuer"},
    {"id":"2","age":"26","name":"Cristiano Ronaldo"},
    {"id":"3","age":"24","name":"Lionel Messi"}
    ]
}

And I have the following Java Object:

public class PlayerDTO implements Serializable {

    private int id;
    private String name;
    private int age;

    //Getters and Setters
}

In my Java class I'm doing:

Type collectionType = new TypeToken<ArrayList<PlayerDTO>>(){}.getType();
List<PlayerDTO> players = gson.fromJson(jsonString, collectionType);

But, when the Gson tries to convert the Json list I receive the error:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
at com.google.gson.Gson.fromJson(Gson.java:817)
at com.google.gson.Gson.fromJson(Gson.java:770)
at com.google.gson.Gson.fromJson(Gson.java:719)
at com.google.gson.Gson.fromJson(Gson.java:691)
at br.com.test.TesteGson.main(TesteGson.java:33)
Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
at com.google.gson.stream.JsonReader.beginArray(JsonReader.java:351)
at com.google.gson.internal.bind.ArrayTypeAdapter.read(ArrayTypeAdapter.java:70)
at com.google.gson.Gson.fromJson(Gson.java:805)
... 4 more

I understand that it happens because the Gson was expecting the Json without the "player" on the beginning of the Json String. Can you help me? Thanks!

5
  • My recommendation is to create a PlayerCollection (?) type which contains an appropriate players list property.. Commented Aug 28, 2014 at 19:54
  • 1
    Anyway, see stackoverflow.com/questions/12004338/… - but these methods seem crufty Commented Aug 28, 2014 at 19:56
  • 1
    It seems gson now comes with streaming api using which you may be able to avoid extra parsing. Commented Aug 28, 2014 at 20:09
  • So you're trying to skip the initial 'player' object and just get the list? Commented Aug 29, 2014 at 14:01
  • No, I wanna know if I'm doing something wrong, because I believe that the Gson is able to convert this Json String in a Java List<Object> Commented Aug 29, 2014 at 14:31

1 Answer 1

1

The easiest way to resolve this issue is to create a Players class to serve as a collection for your players.

Here is the Players class:

import java.util.ArrayList;
import java.util.List;
import com.google.gson.annotations.Expose;

public class Players {

@Expose
private List<Player> players = new ArrayList<Player>();

public List<Player> getPlayer() {
return player;
}

public void setPlayer(List<Player> player) {
this.player = player;
}

}

The Player class:

import com.google.gson.annotations.Expose;

public class Player {

@Expose
private String id;
@Expose
private String age;
@Expose
private String name;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getAge() {
return age;
}

public void setAge(String age) {
this.age = age;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

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

1 Comment

Well, I believe that this is not the best solution. Hoping anyone else gives me another solution.

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.