2016-02-03 67 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