苦労

2016-05-25 28 views
0

I私はJSONをパースする方法を知っていたが、タフな、私は私がしたい値がAndroidのメーカーである私に次のメッセージ与えキーにアクセスしようとすると:苦労

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

が存在してはなりません。私は media_detailsがJSONのどこにも見当たりません。私は 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それはまだあなたがあなたが必要としない1つの余分な呼び出しをしているように見えます: 'getJsonObject(" media_details ")'そのキーはあなたが投稿したJSONに存在しません。その関数にブレークポイントを追加してください。各ステートメントをステップ実行し、各ステップで値をチェックします。 –

+0

@miguelacio私はケビンが正しいと思っています、私もコードを見てきましたし、 'media_details'キーは存在しません。それを取り除き、それなしで試してみてください。 – Vucko