使用gson在Java中反序列化json

R. *_*nje 0 java android json gson

我在用gson反序列化json中的JSON时非常艰难。

我有以下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"}]}
Run Code Online (Sandbox Code Playgroud)

我现在拥有的Java代码是:

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);
        }
    });
Run Code Online (Sandbox Code Playgroud)

WoWRaces.java中的以下代码:

WoWRaces.java

public class WoWRaces {
   public Integer id;
   public String name;
}
Run Code Online (Sandbox Code Playgroud)

它给了我以下错误:

预期为BEGIN_ARRAY,但为BEGIN_OBJECT

我搜索并访问了多个问题,但似乎无法弄清楚。我需要的数据是ID和绑定到它的名称。

先感谢您

小智 7

如果您使用的是gson库,请尝试此操作

    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());
    }
Run Code Online (Sandbox Code Playgroud)

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;
    }

}
Run Code Online (Sandbox Code Playgroud)

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;
    }

}
Run Code Online (Sandbox Code Playgroud)