使用gson在java中反序列化json [英] Deserialize json in java with gson

查看:139
本文介绍了使用gson在java中反序列化json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难用json在java中反序列化json。

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

我有以下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代码是:

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

在WoWRaces.java中是以下代码:

In WoWRaces.java is the following code:

WoWRaces.java

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

它给我以下错误:


预期BEGIN_ARRAY但是BEGIN_OBJECT

Expected BEGIN_ARRAY but was BEGIN_OBJECT

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

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.

提前谢谢

推荐答案

如果您正在使用gson库,那么试试这个

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

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

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

}

这篇关于使用gson在java中反序列化json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆