2017-04-20 40 views
-2

我在使用gson反序列化java中的json時非常困難。使用gson反序列化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; 
} 

它給我下面的錯誤:

Expected BEGIN_ARRAY but was BEGIN_OBJECT

我已搜查,並參觀了多個問題,但我似乎無法想出解決辦法。我需要的數據是id和綁定的名稱。

預先感謝您

+0

你想解析JSON? –

+0

使用gson庫。這將是非常容易的 – Krish

+0

是的,我想解析它,而我正在使用gson,但我似乎無法弄清楚:/ –

回答

2

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

} 
+0

這工作,非常感謝你! –

+0

yes..create ItemResponse和Item class然後你可以做同樣的事情,你以前做過。 – bhaumiksoni

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; 
+0

這是正確的。錯誤'Expected BEGIN_ARRAY but the BEGIN_OBJECT'the SO gets,is because because your typeToken he told Gson expect a array,but the JSON response is a object(contains one array)。這個答案正確地說明了這一點。 – Ridcully

+0

我也會試試這個,謝謝你花時間幫助我! –

+0

謝謝,Upvote它,如果有幫助 – Pehlaj

1

,你可以把你的JSON字符串here和複製應用中的所有類和使用主類在new Gson.fromJson(jsonString,Example.class)

in the url select this option

  1. 源類型:Json的
  2. 註釋風格:GSON