2014-01-06 76 views
-3
package com.example.moviesearch; 

import java.util.ArrayList; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.util.Log; 

public class ParseJSON { 
    private ArrayList<Movie> movies; 
    private String genres; 
    private String rated; 
    private String language; 
    private int rating; 
    private String country; 
    private String release_date; 
    private String title; 
    private String directors; 
    private String actors; 
    private String plot_simple; 
    private String poster; 
    private String runtime; 
    private String imdb_url; 

    public ArrayList<Movie> parseJson(String json) { 
     movies = new ArrayList<Movie>(); 
     try { 
      JSONArray jArray = new JSONArray(json); 
      for (int i = 0; i < jArray.length(); i++) { 
       JSONObject j = jArray.getJSONObject(i); 
       try { 
        genres = j.getString("genres").replace("[", "") 
          .replace("]", "").replaceAll("\"", ""); 
       } catch (Exception e) { 
        genres = "notfound"; 
       } 
       try { 
        rated = j.getString("rated"); 
       } catch (Exception e) { 
        rated = "not found"; 
       } 
       try { 
        language = j.getString("language").replace("[", "") 
          .replace("]", "").replaceAll("\"", ""); 
       } catch (Exception e) { 
        language = "notfound"; 
       } 

       try { 
        rating = j.getInt("rating"); 
       } catch (Exception e) { 
        rating = 404; 
       } 

       try { 
        country = j.getString("country").replace("[", "") 
          .replace("]", "").replaceAll("\"", ""); 
       } catch (Exception e) { 
        country = "not found"; 
       } 
       try { 
        release_date = j.getString("release_date"); 
       } catch (Exception e) { 
        release_date = "notfound"; 
       } 
       try { 
        title = j.getString("title").replace("\n", ""); 
       } catch (Exception e) { 
        title = "notfound"; 
       } 
       try { 
        directors = j.getString("directors").replace("[", "") 
          .replace("]", "").replaceAll("\"", ""); 
       } catch (Exception e) { 
        directors = "notfound"; 
       } 
       try { 
        actors = j.getString("actors").replace("[", "") 
          .replace("]", "").replaceAll("\"", ""); 
       } catch (Exception e) { 
        actors = "notfound"; 
       } 
       try { 
        plot_simple = j.getString("plot_simple"); 
       } catch (Exception e) { 
        poster = "notfound"; 
       } 
       try { 
        JSONObject c = j.getJSONObject("poster"); 
        poster = c.getString("cover"); 
       } catch (Exception e) { 
        poster = "notfound"; 
       } 
       try { 
        runtime = j.getString("runtime").replace("[", "") 
          .replace("]", "").replaceAll("\"", ""); 
       } catch (Exception e) { 
        runtime = "notfound"; 
       } 
       try { 
        imdb_url = j.getString("imdb_url"); 
       } catch (Exception e) { 
        imdb_url = "notfound"; 
       } 
       Movie movie = new Movie(genres, rated, language, rating, 
         country, release_date, title, directors, actors, 
         plot_simple, poster, runtime, imdb_url); 
       movies.add(movie); 
      } 

     } catch (JSONException e) { 
      Log.e("jsonerror", "", e); 
     } 
     return movies; 
    } 
} 

我正在製作一個android應用程序,需要解析json數據。我把這個json數據轉換成一個自定義類。有時我解析的json不存在,所以我必須爲它設置一個默認值,我覺得這太粗暴了。摺疊此代碼的最佳方式?

+2

最好的辦法後,是reg exp。 –

+0

哈哈!不應該有問題應該有*問號*? – Phil

+0

我不明白reg exp會如何幫助。無論哪種方式,我必須檢查JSON是否存在。你能提供一個例子,也許我錯了嗎 – gallly

回答

3

還有一種方法

j.optString("imdb_url", "notfound"); 

在那裏你可以通過一些默認值的情況下的值沒有找到 - 所以你不必捕獲異常每GetString的

+0

哇,我需要感謝 – gallly