正在将 Android 字符串解析为 JSONArray

Parsing Android String to JSONArray

我有一个成功返回的 JSONString,我想将它转换成一个 JSONArray,以便我可以解析这些位。字符串成功返回,但是当我尝试在 "results" 数组内的每部电影对象中记录 "title" 时,它没有出现。

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
    },

尝试更改

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

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

并注释掉上面的日志语句,以便于阅读