2016-05-25 22 views
0

我艱難我知道解析一個JSON的方式,但是當我試圖訪問到我想要的值是Android的工作室給了我下面的消息的關鍵在於:遇到問題解析的源代碼Android Studio中一個JSON陣列

org.JSON.JSONEXCEPTION: no value for large

正如你可以看到它說有是沒有價值的關鍵「大」,但顯然它,反正這是我的JSON解析方法

public static List<News> parseJSONtoNews(JSONArray jsonArray) throws JSONException { 
    List<News> newsList = new ArrayList<>(); 

    for (int i = 0; i < jsonArray.length(); i++) { 
     News news = new News(); 
     JSONObject jsonObjectNews = jsonArray.getJSONObject(i); 

     JSONArray jsonArrayCategories = jsonObjectNews.getJSONArray(JSONKeys.KEY_CATEGORIES); 


     int category = getCatgories(jsonArrayCategories); 
     news.setCategories(category); 

     news.setiD(jsonObjectNews.getInt("id")); 

     JSONObject jsonObjectTitle = jsonObjectNews.getJSONObject("title"); 
     news.setTitle(jsonObjectTitle.getString("rendered")); 

     JSONObject jsonObjectContent = jsonObjectNews.getJSONObject("content"); 
     news.setContent(jsonObjectContent.getString("rendered")); 

     JSONObject jsonObjectImage = jsonObjectNews.getJSONObject("better_featured_image"); 
     JSONObject jsonObjectMediaDetails = jsonObjectImage.getJSONObject("media_details"); 
     JSONObject jsonObjectSizes = jsonObjectMediaDetails.getJSONObject("sizes"); 
     JSONObject jsonObjectMediumLarge = jsonObjectSizes.getJSONObject("large"); 
     news.setImageURL(jsonObjectMediumLarge.getString("source_url")); 
     newsList.add(news); 
    } 



    return newsList; 
} 

public static int getCatgories(JSONArray jsonArray) throws JSONException{ 
    int categories = 0; 
    for (int i = 0; i<jsonArray.length(); i++){ 
     categories = jsonArray.getInt(i); 
    } 

    return categories; 
} 

這是JSON我想分析

[ 
    { 
    "id": 742, 
     "title": { 
     "rendered": 「title」 
    }, 
    "content": { 
     "rendered": 「content」 
    }, 
    "categories": [ 
     4 
    ], 
    "tags": [], 
    "better_featured_image": { 
     "file": "2016/05/20160520_191324.jpg", 
     "sizes": { 
      "thumbnail": { 
      "file": "20160520_191324-150x150.jpg", 
      "source_url": "http://www.aarc.com.mx/wp-content/uploads/2016/05/20160520_191324-150x150.jpg" 
      }, 
      "medium": { 
      "file": "20160520_191324-300x169.jpg", 
      "source_url": "http://www.aarc.com.mx/wp-content/uploads/2016/05/20160520_191324-300x169.jpg" 
      }, 
      "medium_large": { 
      "file": "20160520_191324-768x432.jpg", 
      "source_url": "http://www.aarc.com.mx/wp-content/uploads/2016/05/20160520_191324-768x432.jpg" 
      }, 
      "large": { 
      "file": "20160520_191324-1024x576.jpg", 
      "source_url": "http://www.aarc.com.mx/wp-content/uploads/2016/05/20160520_191324-1024x576.jpg" 
      } 
     }, 
     "post": 742, 
     "source_url": "http://www.aarc.com.mx/wp-content/uploads/2016/05/20160520_191324.jpg" 
    }, 
}, 
{ 
    "id": 745, 
     "title": { 
     "rendered": 「title」 
    }, 
    "content": { 
     "rendered": 「content」 
    }, 
    "categories": [ 
     4 
    ], 
    "tags": [], 
    "better_featured_image": { 
     "file": "2016/05/20160520_191324.jpg", 
     "sizes": { 
      "thumbnail": { 
      "file": "20160520_191324-150x150.jpg", 
      "source_url": "http://www.aarc.com.mx/wp-content/uploads/2016/06/20160520_191324-150x150.jpg" 
      }, 
      "medium": { 
      "file": "20160520_191324-300x169.jpg", 
      "source_url": "http://www.aarc.com.mx/wp-content/uploads/2016/06/20160520_191324-300x169.jpg" 
      }, 
      "medium_large": { 
      "file": "20160520_191324-768x432.jpg", 
      "source_url": "http://www.aarc.com.mx/wp-content/uploads/2016/06/20160520_191324-768x432.jpg" 
      }, 
      "large": { 
      "file": "20160520_191324-1024x576.jpg", 
      "source_url": "http://www.aarc.com.mx/wp-content/uploads/2016/06/20160520_191324-1024x576.jpg" 
      } 
     }, 
     "post": 742, 
     "source_url": "http://www.aarc.com.mx/wp-content/uploads/2016/06/20160520_191324.jpg" 
    }, 
    } 
] 

正如你所看到的那裏存在關鍵「大」,但我不能訪問它,它與「縮略圖」鍵,所以我不知道如何處理這個,我希望你們可以幫助我! 感謝

+0

https://dzone.com/articles/be-lazy-productive-android 使用傑克遜解析JSON,而不是手動解析 –

回答

2

在我看來:

JSONObject jsonObjectImage = jsonObjectNews.getJSONObject("better_featured_media"); 
JSONObject jsonObjectMediaDetails = jsonObjectImage.getJSONObject("media_details"); 

jsonObjectMediaDetails不應該存在。我在JSON的任何地方都看不到media_details。我也看不到better_featured_media。我認爲它應該是better_featured_image

嘗試:

JSONObject jsonObjectImage = jsonObjectNews.getJSONObject("better_featured_image"); 
JSONObject jsonObjectSizes = jsonObjectImage.getJSONObject("sizes"); 
+0

我很抱歉,但我有所有這些鍵在一個名爲JSONKEYS的類中,我寫錯了,但在我的代碼中它說「」better_featured_image「,正如我告訴過你的,我可以訪問htumbnail的鍵,但我不能對大的,(這是我想要的)謝謝你的回覆,我很欣賞它 – miguelacio

+0

@miguelacio它仍然在我看來,你正在做一個額外的電話,你不需要:'getJsonObject(「media_details」)'。該鍵不存在你發佈的JSON。嘗試在該函數中添加一個斷點, d逐步檢查每個語句並檢查每個步驟的值。 –

+0

@miguelacio我認爲凱文是正確的,我也觀察到代碼和'media_details'鍵不存在。刪除那一個,嘗試沒有它。 – Vucko