用gson反编译java中的json

时间:2017-04-20 10:15:39

标签: java android json gson

我很难用json在java中反序列化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"}]}

我现在的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);
        }
    });

在WoWRaces.java中是以下代码:

WoWRaces.java

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

它给了我以下错误:

  

预计BEGIN_ARRAY但是BEGIN_OBJECT

我搜索并访问过多个问题,但我似乎无法弄清楚这一点。我需要的数据是id和绑定它的名称。

提前谢谢

3 个答案:

答案 0 :(得分:5)

如果您正在使用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());
    }

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

}

答案 1 :(得分:1)

1。创建RacesResponse类为:

 public class RacesResponse {

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

2。将您的代码更改为:

RacesResponse racesResponse = gson.fromJson(response, RacesResponse.class);
List<WowRaces> races = racesResponse.list;

答案 2 :(得分:1)

你可以把你的json字符串here复制到应用中的所有类,并在new Gson.fromJson(jsonString,Example.class)

中使用主类
  在网址中

选择此选项

  1. 来源类型:Json
  2. 注释风格:Gson