2016-06-26 16 views
0

我有一個關於從自定義JSON響應轉換爲WP V2 API的WordPress標準JSON響應的問題。從定製切換到WP API V2的JSON響應

我的自定義JSON響應看起來是這樣的 - 它的內

{ 
    "categories": 
    [26] 
    0: 
    { 
    "term_id": 12 
    "name": "Android" 
    "slug": "android" 
    "term_group": 0 
    "term_taxonomy_id": 12 
    "taxonomy": "category" 
    "description": "" 
    "parent": 463 
    "count": 10 
    "filter": "raw" 
    "cat_ID": 12 
    "category_count": 10 
    "category_description": "" 
    "cat_name": "Android" 
    "category_nicename": "android" 
    "category_parent": 463 
    } 
} 

我的代碼看起來像這樣

// Preparing volley's json object request 
    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET, url, null, new Response.Listener<JSONObject>() { 

     @Override 
     public void onResponse(JSONObject response) { 
      List<Category> categories = new ArrayList<Category>(); 

      try { 
       if (response.has("error")) { 
        String error = response.getString("error"); 
        Toast.makeText(getApplicationContext(), error, Toast.LENGTH_LONG).show(); 
       }else { 
        // Parsing the json response 
        JSONArray entry = response.getJSONArray(TAG_CATEGORIES); 

        // loop through categories and add them to 
        // list 
        for (int i = 0; i < entry.length(); i++) { 
         JSONObject catObj = (JSONObject) entry.get(i); 
         // album id 
         String catID = catObj.getString(TAG_TERM_ID); 

         // album title 
         String catTitle = catObj.getString(TAG_TERM_NAME); 

         Category category = new Category(); 
         category.setId(catID); 
         category.setTitle(catTitle); 

         // add album to list 
         categories.add(category); 
        } 

WordPress的RST API V2 JSON響應看起來是這樣的框架:

[10] 
0: { 
    "id": 12 
    "count": 10 
    "description": "" 
    "link": "https://torbjornzetterlund.com/category/technology/mobile-apps/android/" 
    "name": "Android" 
    "slug": "android" 
    "taxonomy": "category" 
    "parent": 463 
    "img": false 
    "_links": { 
     "self": [1] 
     0: { 
      "href": "https://torbjornzetterlund.com/wp-json/wp/v2/categories/12" 
     }- 
     - "collection": [1] 
     0: { 
      "href": "https://torbjornzetterlund.com/wp-json/wp/v2/categories" 
     }- 
     - "about": [1] 
     0: { 
      "href": "https://torbjornzetterlund.com/wp-json/wp/v2/taxonomies/category" 
     }- 
     - "up": [1] 
     0: { 
     "embeddable": true 
     "href": "https://torbjornzetterlund.com/wp-json/wp/v2/categories/463" 
     }- 
     -"wp:post_type": [1] 
     0: { 
     "href": "https://torbjornzetterlund.com/wp-json/wp/v2/posts?categories=12" 
     }- 
     - "curies": [1] 
     0: { 
     "name": "wp" 
     "href": "https://api.w.org/{rel}" 
     "templated": true 
     } 
    } 
    } 
} 

新的JSO響應沒有一個類別稱爲類別,所以我不能使用thi聲明。

 // Parsing the json response 
     JSONArray entry = response.getJSONArray(TAG_CATEGORIES); 

我找不到凌空庫或實例的任何好的文檔,我當前的代碼獲得一個對象從URL,那麼代碼使用response.getJSONArray擺脫對象數組。

如果我不能識別對象內的數組,我需要做什麼,我得到的json響應是一個數組,我應該將對象的請求更改爲數組。我不確定在這一點上有點失落。感謝任何幫助。

回答

0

您正在使用JsonObjectRequest,對於您的api來說什麼是正確的,但現在它來自api的JsonArray。

Volley Toolbox中還有一個JsonArrayRequest。如果您更改請求類型,那麼它將起作用。

如果有什麼不清楚的地方,隨時問。

+0

感謝變化後,我更換了jsonObjectRequest與JsonArrayRequest和它現在工作正常。 – user1557970

+0

不客氣 – Botz

0

這是我的代碼片段看起來像從JsonObjectRequest到JsonArrayRequest JsonArrayRequest jsonArrReq =新JsonArrayRequest(URL,新Response.Listener(){

 @Override 
     public void onResponse(JSONArray response) { 
      Log.d(TAG, response.toString()); 
      List<Category> categories = new ArrayList<Category>(); 

      for (int i = 0; i < response.length(); i++) { 
       try { 
        JSONObject catObj = response.getJSONObject(i); 
        // album id 
        String catID = catObj.getString(TAG_TERM_ID); 

        // album title 
        String catTitle = catObj.getString(TAG_TERM_NAME); 

        Category category = new Category(); 
        category.setId(catID); 
        category.setTitle(catTitle); 

        // add album to list 
        categories.add(category); 

        // Store categories in shared pref 
        AppController.getInstance().getPrefManger().storeCategories(categories); 

        // String the main activity 
        Intent intent = new Intent(getApplicationContext(), MainActivity.class); 
        startActivity(intent); 

       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 

      } 
     } 
    }, new Response.ErrorListener() {