2017-03-06 38 views
0
使用JSON

我试图解析Java中的JSON对象的图像的URL和日期:解析Android中

{ 
     "poster_path": "/e3QX3tY0fBR1GuxdP0mwpN16cqI.jpg", 
     "adult": false, 
     "overview": "In 1950s Pittsburgh, a frustrated African-American father struggles with the constraints of poverty, racism, and his own inner demons as he tries to raise a family.", 
     "release_date": "2016-12-16", 
     "genre_ids": [ 
      18 
     ], 
     "id": 393457, 
     "original_title": "Fences", 
     "original_language": "en", 
     "title": "Fences", 
     "backdrop_path": "/jNlCIAcheh0iOuL3kz9x1Wq9WLG.jpg", 
     "popularity": 10.976374, 
     "vote_count": 290, 
     "video": false, 
     "vote_average": 6.7 
    }, 

但我得到一个JSONException,当我尝试访问值poster_path(org.json .JSONException:对于poster_path无值)和RELEASE_DATE(org.json.JSONException:对于RELEASE_DATE无值)

ArrayList<MovieModel> results = new ArrayList<MovieModel>(); 
      String streamAsString = result; 
      try{ 
       JSONObject jsonObject = new JSONObject(streamAsString); 
       JSONArray array = (JSONArray) jsonObject.get("results"); 
       for (int i = 0; i < array.length(); i++) { 
        JSONObject c = array.getJSONObject(i); 
        JSONObject jsonMovie = array.getJSONObject(i); 
        MovieModel movieModel= new MovieModel(); 
        movieModel.setMovieTitle(jsonMovie.getString("title")); 
        movieModel.setMovieGenre("na");; 
        String strImgURL=jsonObject.getString("poster_path").substring(2); 
        movieModel.setImgURL(""); 
        movieModel.setMovieYear(jsonObject.getString("release_date")); 
        results.add((movieModel)); 
       } 
      } 
      catch(JSONException j) 
      { 
       System.err.println(j); 
       Log.d(DEBUG_TAG, "Error parsing JSON. String was: " + j.toString()); 
      } 

不知道什么可能会造成这个错误

+0

我没有看到一个结果阵列 – zombie

回答

3

你试图让poster_pathrelease_date来自外部JSONObject的值,它具有一切(包括包含单独电影的JSONArray)。

在循环中,只使用jsonMovie代替jsonObject

for (int i = 0; i < array.length(); i++) { 
    JSONObject jsonMovie = array.getJSONObject(i); 
    MovieModel movieModel= new MovieModel(); 
    movieModel.setMovieTitle(jsonMovie.getString("title")); 
    movieModel.setMovieGenre("na"); 
    String strImgURL=jsonMovie.getString("poster_path").substring(2); 
    movieModel.setImgURL(""); 
    movieModel.setMovieYear(jsonMovie.getString("release_date")); 
    results.add((movieModel)); 
} 
+0

谢谢...它的工作 – napoleon