小编R. *_*nje的帖子

使用gson在Java中反序列化json

我在用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 …
Run Code Online (Sandbox Code Playgroud)

java android json gson

0
推荐指数
1
解决办法
2882
查看次数

标签 统计

android ×1

gson ×1

java ×1

json ×1