2017-07-24 20 views
0

我是JSON的新手,我正在面臨艱難的時間循環JSONArray內的JSONArray爲我正在構建的Android應用程序。我會很感激,如果海報會給出解決方案的簡要解釋,因爲我只是不想複製&粘貼代碼,但瞭解它。謝謝。WordPress的API - 如何循環通過JSON Android/Java中的對象數組

API鏈接:https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/categories

API輸出:

[ 
{ 
    "id": 5580, 
    "count": 5, 
    "description": "", 
    "link": "https://spoketest.wordpress.com/category/diversions/", 
    "name": "Diversions", 
    "slug": "diversions", 
    "taxonomy": "category", 
    "parent": 0, 
    "meta": [], 
    "_links": { 
     "self": [ 
      { 
       "href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/categories/5580" 
      } 
     ], 
     "collection": [ 
      { 
       "href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/categories" 
      } 
     ], 
     "about": [ 
      { 
       "href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/taxonomies/category" 
      } 
     ], 
     "wp:post_type": [ 
      { 
       "href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/posts?categories=5580" 
      } 
     ], 
     "curies": [ 
      { 
       "name": "wp", 
       "href": "https://api.w.org/{rel}", 
       "templated": true 
      } 
     ] 
    } 
}, 
{ 
    "id": 9548, 
    "count": 5, 
    "description": "", 
    "link": "https://spoketest.wordpress.com/category/features/", 
    "name": "Features", 
    "slug": "features", 
    "taxonomy": "category", 
    "parent": 0, 
    "meta": [], 
    "_links": { 
     "self": [ 
      { 
       "href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/categories/9548" 
      } 
     ], 
     "collection": [ 
      { 
       "href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/categories" 
      } 
     ], 
     "about": [ 
      { 
       "href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/taxonomies/category" 
      } 
     ], 
     "wp:post_type": [ 
      { 
       "href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/posts?categories=9548" 
      } 
     ], 
     "curies": [ 
      { 
       "name": "wp", 
       "href": "https://api.w.org/{rel}", 
       "templated": true 
      } 
     ] 
    } 
}, 
{ 
    "id": 103, 
    "count": 10, 
    "description": "", 
    "link": "https://spoketest.wordpress.com/category/news/", 
    "name": "News", 
    "slug": "news", 
    "taxonomy": "category", 
    "parent": 0, 
    "meta": [], 
    "_links": { 
     "self": [ 
      { 
       "href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/categories/103" 
      } 
     ], 
     "collection": [ 
      { 
       "href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/categories" 
      } 
     ], 
     "about": [ 
      { 
       "href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/taxonomies/category" 
      } 
     ], 
     "wp:post_type": [ 
      { 
       "href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/posts?categories=103" 
      } 
     ], 
     "curies": [ 
      { 
       "name": "wp", 
       "href": "https://api.w.org/{rel}", 
       "templated": true 
      } 
     ] 
    } 
}, 
{ 
    "id": 660, 
    "count": 10, 
    "description": "", 
    "link": "https://spoketest.wordpress.com/category/people/", 
    "name": "People", 
    "slug": "people", 
    "taxonomy": "category", 
    "parent": 0, 
    "meta": [], 
    "_links": { 
     "self": [ 
      { 
       "href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/categories/660" 
      } 
     ], 
     "collection": [ 
      { 
       "href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/categories" 
      } 
     ], 
     "about": [ 
      { 
       "href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/taxonomies/category" 
      } 
     ], 
     "wp:post_type": [ 
      { 
       "href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/posts?categories=660" 
      } 
     ], 
     "curies": [ 
      { 
       "name": "wp", 
       "href": "https://api.w.org/{rel}", 
       "templated": true 
      } 
     ] 
    } 
}, 
{ 
    "id": 318, 
    "count": 10, 
    "description": "", 
    "link": "https://spoketest.wordpress.com/category/tech/", 
    "name": "Tech", 
    "slug": "tech", 
    "taxonomy": "category", 
    "parent": 0, 
    "meta": [], 
    "_links": { 
     "self": [ 
      { 
       "href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/categories/318" 
      } 
     ], 
     "collection": [ 
      { 
       "href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/categories" 
      } 
     ], 
     "about": [ 
      { 
       "href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/taxonomies/category" 
      } 
     ], 
     "wp:post_type": [ 
      { 
       "href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/posts?categories=318" 
      } 
     ], 
     "curies": [ 
      { 
       "name": "wp", 
       "href": "https://api.w.org/{rel}", 
       "templated": true 
      } 
     ] 
    } 
}, 
{ 
    "id": 160479, 
    "count": 6, 
    "description": "", 
    "link": "https://spoketest.wordpress.com/category/traction/", 
    "name": "Traction", 
    "slug": "traction", 
    "taxonomy": "category", 
    "parent": 0, 
    "meta": [], 
    "_links": { 
     "self": [ 
      { 
       "href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/categories/160479" 
      } 
     ], 
     "collection": [ 
      { 
       "href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/categories" 
      } 
     ], 
     "about": [ 
      { 
       "href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/taxonomies/category" 
      } 
     ], 
     "wp:post_type": [ 
      { 
       "href": "https://public-api.wordpress.com/wp/v2/sites/spoketest.wordpress.com/posts?categories=160479" 
      } 
     ], 
     "curies": [ 
      { 
       "name": "wp", 
       "href": "https://api.w.org/{rel}", 
       "templated": true 
      } 
     ] 
    } 
} 
] 

我的代碼:

import org.json.JSONException; 
import org.json.JSONObject; 

public class Category { 
private int mCategoryID; 
private String mCategoryName; 

public static Category fromJSON(JSONArray jsonArray) { 
    try { 
     Category category = new Category(); 
     category.mCategoryID = jsonArray.getJSONObject("0").getInt("id"); 
     category.mCategoryName = jsonArray.getJSONObject("0").getString("name"); 

     return category; 
    } catch (JSONException e) { 
     e.printStackTrace(); 
     return null; 
    } 
} 

public int getCategoryID() { 
    return mCategoryID; 
} 

public String getCategoryName() { 
    return mCategoryName; 
} 
} 

回答

1

一旦檢索來自API的響應(使用AsyncTask),tak e響應文本並創建一個JSONArray。然後,您可以遍歷數組並提取類別。

public void onApiResponse(String response) { 
    // Create a JSONArray from the response 
    JSONArray jsonArray = new JSONArray(response); 

    // Create a structure to store all of the categories 
    List<Category> categories = new ArrayList<Category>(); 

    // Loop through the array and parse all the categories 
    for (int i = 0; i < jsonArray.length(); i++) { 
     // Extract the JSONObject from the index 
     JSONObject jsonCategory = jsonArray.getJSONObject(i); 

     // Add the category to the list 
     categories.add(Category.fromJSON(jsonCategory)); 
    } 

    // Do something with your categories... 
} 

編輯:我看你硬編碼一些值到您的fromJSON,所以我想我會發布的一個修正版本,以及。

public static Category fromJSON(JSONObject jsonObject) { 
    try { 
     Category category = new Category(); 
     category.mCategoryID = jsonObject.getInt("id"); 
     category.mCategoryName = jsonObjectgetString("name"); 

     return category; 
    } catch (JSONException e) { 
     e.printStackTrace(); 
     return null; 
    } 
} 
+0

我使用的詹姆斯·史密斯的Android異步HTTP客戶端庫異步相關的任務([鏈接](https://loopj.com/android-async-http/)),它返回的JSONObject。我的fromJSON方法將該JSONObject作爲解析數據的輸入參數。現在在你的解決方案中,我需要一個字符串作爲輸入參數。我可以使用toString()將JSONObject轉換爲String而沒有任何不良影響嗎? –

+0

您發佈的示例響應是JSON數組,而不是JSON對象。如果您接收到JSON對象作爲響應,則該庫不會正確解釋響應,或者您錯誤地解釋返回值。打印出'JSONObject'的'toString()',並與API的實際文本進行比較,以查看錯誤。 –

+0

哦!我犯了一個錯誤。是的,我得到一個JSONArray,fromJSON的輸入參數是JSONArray。但是,我可以在JSONArray上使用toString而沒有任何不良影響嗎? –