2016-06-12 14 views
0

私はアンドロイドプロジェクトに取り組んでいます.Jsonが必要とするデータに従って複数の画像を表示したい、jsonはネストされた配列で構成されています。アンドロイドスタジオで入れ子にされたjsonを解析する方法は?

[ 
     { 
       "post_images":[ 
        { 
         "message_image_id":"17", 
         "image_name":"rlvezdcuorfuhkr8nluc", 
          "cdn_public_id":null, 
         "image_description":null, 
         "message_id":"24", 
          "width":"960", 
         "height":"959", 
         "comment_count":"1",  
         "like_count":"0", 
         "unlike_count":"0", 
         "share_count":"0", 
          "description":null, 
         "is_shared_product":"0", 
         "shared_with":"1", 
        } 
       ] 
     } 
     { 
      "post_images":[ 
        { 
         "message_image_id":"9", 
          "image_name":"pleyat8ghaaocvipvre2", 
          "cdn_public_id":null, 
         "image_description":null, 
         "message_id":"23", 
          "width":"596", 
         "height":"411", 
          "comment_count":"0", 
         "like_count":"0", 
         "unlike_count":"0", 
         "share_count":"0", 
          "description":null, 
         "is_shared_product":"0",  
         "shared_with":"1", 
        } 
        { 
         "message_image_id":"10", 
          "image_name":"jjtcdum8kksseuo5cuaa", 
          "cdn_public_id":null, 
         "image_description":null, 
         "message_id":"23", 
          "width":"588", 
         "height":"567", 
         "comment_count":"0", 
         "like_count":"0", 
         "unlike_count":"0", 
          "share_count":"0", 
         "description":null, 
         "is_shared_product":"0", 
          "shared_with":"1", 
        } 
        { 
         "message_image_id":"11", 
         "image_name":"w6guovyarum7cnmfxn1u", 
          "cdn_public_id":null, 
         "image_description":null, 
         "message_id":"23", 
          "width":"960", 
         "height":"768", 
          "comment_count":"0",   
         "like_count":"0", 
         "unlike_count":"0", 
         "share_count":"0", 
          "description":null, 
         "is_shared_product":"0", 
         "shared_with":"1", 
          "is_classified_images":"0" 
        } 
        { 
         "message_image_id":"12", 
         "image_name":"cir6zphicl6lq4iirz7h", 
          "cdn_public_id":null, 
         "image_description":null, 
         "message_id":"23", 
          "width":"900", 
         "height":"558", 
         "comment_count":"0", 
         "like_count":"0", 
         "unlike_count":"0", 
         "share_count":"0", 
          "description":null, 
         "is_shared_product":"0", 
         "shared_with":"1", 
         } 
       ] 
      } 
     { 
       "post_images":[ 
        { 
         "message_image_id":"6", 
         "image_name":"mhajt2jifnkucrmgsk6v", 
          "cdn_public_id":null, 
         "image_description":null, 
         "message_id":"22", 
          "width":"547", 
         "height":"590", 
         "comment_count":"1", 
         "like_count":"0",  
         "unlike_count":"0", 
         "share_count":"0", 
          "description":null, 
         "is_shared_product":"0",  
         "shared_with":"1", 
        } 
        { 
         "message_image_id":"7", 
         "image_name":"sxdfvo7qfvewmm85ufsf", 
          "cdn_public_id":null, 
         "image_description":null, 
         "message_id":"22", 
          "width":"596","height":"547", 
         "comment_count":"0",  
         "like_count":"0", 
         "unlike_count":"0", 
         "share_count":"0", 
          "description":null, 
         "is_shared_product":"0", 
         "shared_with":"1", 
        } 
        { 
         "message_image_id":"8", 
         "image_name":"mrvzu4npy8a4ydl8rsnk", 
          "cdn_public_id":null, 
         "image_description":null, 
         "message_id":"22", 
          "width":"826","height":"562", 
         "comment_count":"0",  
         "like_count":"0", 
         "unlike_count":"0", 
         "share_count":"0", 
          "description":null,  
         "is_shared_product":"0", 
         "shared_with":"1", 
         } 
      ] 
     } 

    ] 

答えて

0

あなたはgsonを使用することができ、ここであなたが説明を見つけることができます....モデルクラスにネストされた配列データを解析する方法とベースア​​ダプタクラス 以下の私のJSONがあるを使用してカスタムレイアウトで画像を表示する方法を提案してくださいそれを使用する方法の:Tutorial: Using GSON to Fetch and Parse JSON into Java Models for Android。 幸運を祈る!

0

私は例の助けを借りてこれを説明しますが、これは私のJSONであると言う:

{ 
    "page":1, 
    "results":[ 
     { 
     "poster_path":"\/sM33SANp9z6rXW8Itn7NnG1GOEs.jpg", 
     "adult":false, 
     "overview":"In the animal city of Zootopia, a fast-talking fox who's trying to make it big goes on the run when he's framed for a crime he didn't commit. Zootopia's top cop, a self-righteous rabbit, is hot on his tail, but when both become targets of a conspiracy, they're forced to team up and discover even natural enemies can become best friends.", 
     "release_date":"2016-02-11", 
     "genre_ids":[ 
      16, 
      12, 
      10751, 
      35 
     ], 
     "id":269149, 
     "original_title":"Zootopia", 
     "original_language":"en", 
     "title":"Zootopia", 
     "backdrop_path":"\/mhdeE1yShHTaDbJVdWyTlzFvNkr.jpg", 
     "popularity":90.776684, 
     "vote_count":1099, 
     "video":false, 
     "vote_average":7.51 
     } 
    ], 
    "total_results":19704, 
    "total_pages":986 
} 

私は、次のようJSONObjectとJSONArrayを使用してそれを解析します:

private ArrayList<MovieDataModel> parseJsonResponse(String jsonData) throws JSONException 
     { 
      final String RESULTS_LIST = "results"; 
      final String MOVIE_OVERVIEW = "overview"; 
      final String MOVIE_TITLE = "title"; 
      final String MOVIE_IMAGE_THUMBNAIL = "poster_path"; 
      final String MOVIE_IMAGE = "backdrop_path"; 
      final String VOTE_AVERAGE = "vote_average"; 
      final String RELEASE_DATE = "release_date"; 


      ArrayList<MovieDataModel> movieDataItems = new ArrayList<>(); 

      JSONObject moviesJsonObject = new JSONObject(jsonData); 
      JSONArray movieResults = moviesJsonObject.getJSONArray(RESULTS_LIST); 
      for (int i = 0; i < movieResults.length(); i++) { 
       StringBuffer IMAGE_URL_SMALL = new StringBuffer(context.getString(R.string.image_url_small)); 
       StringBuffer IMAGE_URL_LARGE = new StringBuffer(context.getString(R.string.image_url_large)); 
       JSONObject jsonMovieDataObject = movieResults.getJSONObject(i); 
       MovieDataModel movieDataObject = new MovieDataModel(); 
       movieDataObject.setMovieName(jsonMovieDataObject.getString(MOVIE_TITLE)); 
       movieDataObject.setMoviePlotSynopsis(jsonMovieDataObject.getString(MOVIE_OVERVIEW)); 
       movieDataObject.setMovieImage(IMAGE_URL_LARGE.append(jsonMovieDataObject.getString(MOVIE_IMAGE)).toString()); 
       movieDataObject.setReleaseDate(jsonMovieDataObject.getString(RELEASE_DATE)); 
       movieDataObject.setVoteAverage(jsonMovieDataObject.getString(VOTE_AVERAGE)); 
       movieDataObject.setMoviePosterImageThumbnail((IMAGE_URL_SMALL.append(jsonMovieDataObject.getString(MOVIE_IMAGE_THUMBNAIL))).toString()); 
       movieDataItems.add(movieDataObject); 
      } 

      return movieDataItems; 
     } 
0

あなたにJSONレスポンスを確認してくださいそれは有効ではありません。 http://jsonlint.com/にチェックを入れてください(あなたのレスポンスをコピーしてそこに貼り付け、チェックしてください)。

ここでは、JSONレスポンスの画像パスをすべて取得することができます。 BaseAdapterとImageViewのために

はちょうど

private void method() throws JSONException { 


    JSONArray mainArray = new JSONArray("YOUR JSON RESPONSE"); 
    JSONArray Array1 = null; 
    JSONArray Array2 = null; 

    ArrayList<String> mImageArrayList = new ArrayList<String>(); 

    for (int i = 0; i < mainArray.length(); i++) { 

     JSONObject mObject = mainArray.getJSONObject(i); 

     Array1 = mObject.getJSONArray("post_images"); 

     for (int j = 0; j < Array1.length(); j++) { 


      JSONObject mObject2 = Array1.getJSONObject(i); 

      // here you get image path or name 
      String imagePath = mObject2.getString("image_name"); 
      mImageArrayList.add(imagePath); 




     } 


    } 


} 
" http://theopentutorials.com/tutorials/android/listview/android-custom-listview-with-image-and-text-using-baseadapter/"

JSONの解析を経ます

関連する問題