2015-11-25 40 views
6

我一直無法找到這個問題的詳細答案,或者至少沒有我能理解的答案。如何使用Volley獲取和解析JSON對象

我想設置齊射從iTunes中拉下JSON對象。然後我想分析這些對象,以獲得它們的圖像URL。

因此,舉例來說,這裏是上午iTunes的JSON對象URL

String url = "https://itunes.apple.com/search?term=michael+jackson"; 

所以在這裏(當然使用教程)

String url = "https://itunes.apple.com/search?term=michael+jackson"; 

JsonObjectRequest jsonRequest = new JsonObjectRequest 
     (Request.Method.GET, url, null, new Downloader.Response.Listener // Cannot resolve symbol Listener 
       <JSONObject>() { 
      @Override 
      public void onResponse(JSONObject response) { 
       // the response is already constructed as a JSONObject! 
       try { 
        response = response.getJSONObject("args"); 
        String site = response.getString("site"), 
          network = response.getString("network"); 
        System.out.println("Site: "+site+"\nNetwork: "+network); 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
      } 
     }, new Downloader.Response.ErrorListener // Cannot resolve symbol ErrorListener 
       () { 

      @Override 
      public void onErrorResponse(VolleyError error) { 
       error.printStackTrace(); 
      } 
     }); 

Volley.newRequestQueue(this).add(jsonRequest); 

的我已經建立了我的代碼來獲得這個對象最後的聲明是

Volley.newRequestQueue(this).add(jsonRequest); 

大概,我現在有JSON對象?但是,我如何訪問和解析它?

回答

11

隨着您的網址,你可以使用下面的示例代碼:

JSONObject obj = new JSONObject(gson.toJson(song)); 

通過只需添加到您的build.gradle

 RequestQueue requestQueue = Volley.newRequestQueue(this); 
     String url = "https://itunes.apple.com/search?term=michael+jackson"; 
     JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, new Response.Listener<JSONObject>() { 
      @Override 
      public void onResponse(JSONObject response) { 
       if (response != null) { 
        int resultCount = response.optInt("resultCount"); 
        if (resultCount > 0) { 
         Gson gson = new Gson(); 
         JSONArray jsonArray = response.optJSONArray("results"); 
         if (jsonArray != null) { 
          SongInfo[] songs = gson.fromJson(jsonArray.toString(), SongInfo[].class); 
          if (songs != null && songs.length > 0) { 
           for (SongInfo song : songs) { 
            Log.i("LOG", song.trackViewUrl); 
           } 
          } 
         } 
        } 
       } 
      } 
     }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       Log.e("LOG", error.toString()); 
      } 
     }); 
     requestQueue.add(jsonObjectRequest); 

The SongInfo class:

public class SongInfo { 
    public String wrapperType; 
    public String kind; 
    public Integer artistId; 
    public Integer collectionId; 
    public Integer trackId; 
    public String artistName; 
    public String collectionName; 
    public String trackName; 
    public String collectionCensoredName; 
    public String trackCensoredName; 
    public String artistViewUrl; 
    public String collectionViewUrl; 
    public String trackViewUrl; 
    public String previewUrl; 
    public String artworkUrl30; 
    public String artworkUrl60; 
    public String artworkUrl100; 
    public Float collectionPrice; 
    public Float trackPrice; 
    public String releaseDate; 
    public String collectionExplicitness; 
    public String trackExplicitness; 
    public Integer discCount; 
    public Integer discNumber; 
    public Integer trackCount; 
    public Integer trackNumber; 
    public Integer trackTimeMillis; 
    public String country; 
    public String currency; 
    public String primaryGenreName; 
    public String radioStationUrl; 
    public Boolean isStreamable; 
} 

內的build.gradle文件:

compile 'com.mcxiaoke.volley:library:1.0.19' 
compile 'com.google.code.gson:gson:2.5' 

希望這有助於!

+1

它確實有幫助!非常感謝你。 –

0

只需將該URL粘貼到您的瀏覽器中,即可看到所有的json對象。您可以使用json格式器網站以很好的格式查看。

看看這裏找到你需要的方法。 http://developer.android.com/reference/org/json/JSONObject.html

你的代碼不工作,因爲這個對象不存在於這個json上。

0

與簡單的POJO一起使用GSON。這裏是GSON Documentation

假設你有這樣的:

public class Song{ 
    private String site; 
    private String network; 

    public void setSite(String site){ 
     this.site = site; 
    } 
    public void setNetwork(String network{ 
     this.network = network; 
    } 

    //Add getters as well... 
} 

您可以使用GSON做到這一點:

Song song = Gson.fromJson(response.getJSONObject("args"), Song.class); 

現在你表示響應的對象!請注意我是如何讓「Song」對象的字段名稱與您所關心的值名稱相同的(在這種情況下,它顯示網絡和站點是您想要的)。 Gson執行將JSON對象序列化到POJO的工作,您可以乾淨地直接訪問值,並且更容易。

轉換回它是那樣簡單:

compile 'com.google.code.gson:gson:1.7.2'