0

I am having a very tough time deserializing json in java with gson.

I have the following json:

{"races":[
{"id":1,"mask":1,"side":"alliance","name":"Human"},
{"id":2,"mask":2,"side":"horde","name":"Orc"},
{"id":3,"mask":4,"side":"alliance","name":"Dwarf"}]}

The java code I have now is:

StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Gson gson = new Gson();
                    Type type = new TypeToken<List<WoWDetails>>(){}.getType();
                    List<WoWRaces> races = gson.fromJson(response, type);
                    for (WoWRaces race : races){
                        if(raceID.equals(race.id)) {
                            raceName = race.name;
                        }
                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            errorMSG = (TextView) findViewById(R.id. textView5);
            errorMSG.setText("That didn't work! URL: \n"+error);
            errorMSG.setVisibility(View.VISIBLE);
        }
    });

In WoWRaces.java is the following code:

WoWRaces.java

public class WoWRaces {
   public Integer id;
   public String name;
}

It's giving me the following error:

Expected BEGIN_ARRAY but was BEGIN_OBJECT

I have searched and visited multiple questions but I can't seem to figure this out. The data that I would need is the id and the name bound to it.

Thank you in advance

PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
R. Montanje
  • 63
  • 1
  • 7

3 Answers3

9

If you are using gson library then try this

    Gson gson = new Gson();
    MainResponse mainResponse = gson.fromJson(response, MainResponse.class);
    List<Race> races = mainResponse.getRaces();
    for (Race race : races) {
        Log.e("TEST","Race id : " + race.getId());
        Log.e("TEST","Race Name : " + race.getName());
    }

MainResponse.java

public class MainResponse {

    @SerializedName("races")
    @Expose
    private List<Race> races = null;

    public List<Race> getRaces() {
        return races;
    }

    public void setRaces(List<Race> races) {
        this.races = races;
    }

}

Race.java

public class Race {

    @SerializedName("id")
    @Expose
    private int id;
    @SerializedName("mask")
    @Expose
    private int mask;
    @SerializedName("side")
    @Expose
    private String side;
    @SerializedName("name")
    @Expose
    private String name;

    public int getId() {
        return id;
    }

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

    public int getMask() {
        return mask;
    }

    public void setMask(int mask) {
        this.mask = mask;
    }

    public String getSide() {
        return side;
    }

    public void setSide(String side) {
        this.side = side;
    }

    public String getName() {
        return name;
    }

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

}
bhaumiksoni
  • 244
  • 1
  • 7
1

1. Create RacesResponse class as:

 public class RacesResponse {

     @SerializedName("races")
     public List<WowRaces> list;
 }

2. Change your code to:

RacesResponse racesResponse = gson.fromJson(response, RacesResponse.class);
List<WowRaces> races = racesResponse.list;
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
  • This is correct. The error 'Expected BEGIN_ARRAY but was BEGIN_OBJECT' the SO gets, is because via your TypeToken he tells Gson to expect an array, but the JSON response is an object (containing one array). This answer here adresses this correctly. – Ridcully Apr 20 '17 at 10:40
  • I will try this too, thank you for taking your time to help me! – R. Montanje Apr 20 '17 at 11:23
  • I can't upvote yet, I just made this account, I will when I can! – R. Montanje Apr 20 '17 at 12:12
1

you can put your json string here and copy all classes in your app and use main class to in the new Gson.fromJson(jsonString,Example.class)

in the url select this option

  1. Source type:Json
  2. Annotation style:Gson
Bhupat Bheda
  • 1,968
  • 1
  • 8
  • 13