簡體   English   中英

Android-如何從api.rottentomatoes解析JSONObject和JSONArrays

[英]Android - How to parse JSONObject and JSONArrays from api.rottentomatoes

因此,有此JSON代碼。 我正在嘗試獲取“ abridged_cast”。 但它很復雜。 其JSONObject位於JSONArray內,而jSONObject位於JsonArray內。...

{
    "total": 591,
    "movies": [
        {
            "title": "Jack and Jill",
            "year": 2011,
            "runtime": "",
            "release_dates": {
                "theater": "2011-11-11"
            },
            "ratings": {
                "critics_score": -1,
                "audience_score": 90
            },
            "synopsis": "",
            "posters": {
                "thumbnail": "",
                "profile": "",
                "detailed": "",
                "original": ""
            },
            "abridged_cast": [
                {
                    "name": "Al Pacino",
                    "characters": []
                },
                {
                    "name": "Adam Sandler",
                    "characters": []
                },
                {
                    "name": "Katie Holmes",
                    "characters": []
                }
            ],
            "links": {
                "self": "",
                "alternate": ""
            }
        }
    ],
    "links": {
        "self": "",
        "next": ""
    },
    "link_template": ""
}

這是我獲取“標題”和“年份”的代碼

if (response != null) {
            try {
                // convert the String response to a JSON object,
                // because JSON is the response format Rotten Tomatoes uses
                JSONObject jsonResponse = new JSONObject(response);

                // fetch the array of movies in the response
                JSONArray movies = jsonResponse.getJSONArray("movies");

                // add each movie's title to an array
                movieTitles = new String[movies.length()];
                for (int i = 0; i < movies.length(); i++) {
                    JSONObject movie = movies.getJSONObject(i);
                    movieTitles[i] = movie.getString("title");
                }

希望有人會幫助我,因為我不知道如何獲得abridged_cast“

try {
                 String Movie = null;
                 String abridged = null;
                JSONArray jsonResponse = new JSONArray(response);
                 for (int i = 0; i< jsonResponse.length(); i++) {
                        Movie     =   jsonResponse.getJSONObject(i).getString("movies").toString();
                        System.out.println("movies="+Movie);
                       abridged =   jsonResponse.getJSONObject(i).getString("abridged_cast").toString();
                 }


                 JSONArray jArray = new JSONArray(Movie);
                 for (int i = 0; i< jArray.length(); i++) {
                     String title       =   jArray.getJSONObject(i).getString("title").toString();
                     System.out.println("title="+title);

                 }

                JSONArray jabridgeArray = new JSONArray(abridged);
                 for (int i = 0; i< jabridgeArray.length(); i++) {
                     String title       =   jabridgeArray.getJSONObject(i).getString("name").toString();
                     System.out.println("title="+title);

                 }

            } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

movies包含一系列“電影”對象。 這些對象中的每個對象都包含abridged_cast字段,該字段是(稱為“ cast member”對象)對象的數組。

如果您不打算映射到POJO而是通過JSON,則只需在獲取movie之后在循環中獲取該數組,然后使用另一個方法以相同的方式從該數組中獲取每個“ cast member”對象環。

...
JSONArray cast = movie.getJSONArray("abridged_cast");
for (int j = 0; j < cast.length(); j++) {
    JSONObject castMember = cast.getJSONObject(j);
    ... 
}

從評論中編輯:您最初的問題涉及如何從擁有的JSON中提取信息; 上面的代碼解釋了這一點。 現在看來,您正在詢問有關如何使用它的更基本的編程問題。

如果您要使用Android隨附的包含的org.json類,那么您現在知道如何訪問返回的JSON對象中的信息。 您可以使用json.org包中的對象和方法圍繞解析的JSONObject編寫方法以按原樣訪問數據。 例如,您可以編寫一個“ getMovie()”方法,該方法將電影的名稱作為字符串,然后在“電影”數組中搜索正確的電影,然后將其作為JSONObject返回。

通常,您將使用Java創建類,以封裝該JSON中返回的數據,並使用適合於自己的訪問模式的數據結構(例如,使用名稱作為關鍵字包含所有電影的Map )。 使用org.json類,您將必須實例化這些對象並像在問題中一樣在解析JSON時手動填充它們。 如果您使用了GsonJackson JSON解析庫,則它們能夠獲取您擁有的JSON,並將所有數據映射到您創建的類,並在一次調用中返回它們。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM