2016-02-03 112 views
-2

我有一個成功回來的JSONString,我想將它轉換成JSONArray,我可以解析這些位。字符串成功返回,但是當我嘗試在「結果」數組內的每個電影對象內記錄「標題」時,它不會出現。解析Android字符串到JSONArray

public List<MovieItem> fetchItems() { 

List<MovieItem> items = new ArrayList<>(); 

try { 
    String url = Uri.parse("https://api.themoviedb.org/3/movie/popular") 
      .buildUpon() 
      .appendQueryParameter("api_key", API_KEY) 

      .build().toString(); 


    String jsonString = getUrlString(url); 
    Log.i(TAG, "Received JSON: " + jsonString); 


    JSONObject jsonBody = new JSONObject(jsonString); 
    JSONArray photoJsonArray = jsonBody.getJSONArray("results"); 


    for(int i = 0; i < photoJsonArray.length(); i++){ 

     JSONObject jsonPart = photoJsonArray.getJSONObject(i); 

     Log.i("title", jsonPart.getString("title")); 
    } 


} catch (IOException ioe) { 
    Log.e(TAG, "Failed to fetch items", ioe); 
} catch (JSONException je) { 
    Log.e(TAG, "Failed to parse", je); 
} 
return items; 
} 

-

02-03 13:34:43.236 4728-4746/? I/MovieFetchr: Received JSON: { 
"page":1, 
"results":[ 
    { 
     "poster_path":"\/oXUWEc5i3wYyFnL1Ycu8ppxxPvs.jpg", 
     "adult":false, 
     "overview":"In the 1820s, a frontiersman, Hugh Glass, sets out on a path of vengeance against those who left him for dead after a bear mauling.", 
     "release_date":"2015-12-25", 
     "genre_ids":[37,18,12,53], 
     "id":281957, 
     "original_title":"The Revenant", 
     "original_language":"en", 
     "title":"The Revenant", 
     "backdrop_path":"\/uETWtwsE1QjfoFqRQqFLnSjppPA.jpg", 
     "popularity":42.096309, 
     "vote_count":1079, 
     "video":false, 
     "vote_average":7.36 
    }, 
    { 
     "poster_path":"\/kqjL17yufvn9OVLyXYpvtyrFfak.jpg", 
     "adult":false, 
     "overview":"An apocalyptic story set in the furthest reaches of our planet, in a stark desert landscape where humanity is broken, and most everyone is crazed fighting for the necessities of life. Within this world exist two rebels on the run who just might be able to restore order. There's Max, a man of action and a man of few words, who seeks peace of mind following the loss of his wife and child in the aftermath of the chaos. And Furiosa, a woman of action and a woman who believes her path to survival may be achieved if she can make it across the desert back to her childhood homeland.", 
     "release_date":"2015-05-13", 
     "genre_ids":[878,53,28,12], 
     "id":76341, 
     "original_title":"Mad Max: Fury Road", 
     "original_language":"en", 
     "title":"Mad Max: Fury Road", 
     "backdrop_path":"\/tbhdm8UJAb4ViCTsulYFL3lxMCd.jpg", 
     "popularity":32.157869, 
     "vote_count":3566, 
     "video":false, 
     "vote_average":7.5 
    }, 
+0

你在這裏得到任何錯誤? – Rohit5k2

+0

我沒有收到錯誤。 Genymotion模擬器加載正常。如圖所示,JSON字符串恢復正常。只是當我將JSONstring轉換爲jsonArray時,它不會記錄單個標題。謝謝。 –

+0

看起來一切都好。你嘗試調試嗎? – Rohit5k2

回答

0

嘗試改變

Log.i("title", jsonPart.getString("title")); 

Log.i(TAG, "Title: " + jsonPart.getString("title")); 

並註釋掉上面的日誌語句,以便它可以很容易地

閱讀
+0

我覺得這個答案應該被標記爲接受。 – Rohit5k2