如何只从json获取我们想要的特定数据

时间:2016-04-23 05:35:31

标签: java android json

我无法加载somedata,我已经是数据库并且查询名称为“tipe”(类型),在查询中tipe只有2种类型的字符串(“1”和“2”) 那么,如果我只想加载特定数据tipe =“1”....

,如何加载数据

这是我在android中的代码:

MovieFragment.java

                        // Parsing json
                        for (int i = 0; i < response.length(); i++) {
                            try {

                                JSONObject obj = response.getJSONObject(i);
                                Movie movie = new Movie();
                                movie.setTitle(obj.getString("name"));
                                movie.setThumbnailUrl(obj.getString("images1"));
                                //movie.setDescribe(obj.getString("describe"));
                                //movie.setRating(((Number) obj.get("rating"))
                                //        .doubleValue());
                                movie.setYear(obj.getInt("id"));
                                movie.setTipe(obj.getString("tipe"));

                                /*// Genre is json array
                                JSONArray genreArry = obj.getJSONArray("genre");
                                ArrayList<String> genre = new ArrayList<String>();
                                for (int j = 0; j < genreArry.length(); j++) {
                                    genre.add((String) genreArry.get(j));
                                }
                                movie.setGenre(genre);*/

                                // adding movie to movies array
                                movieList.add(movie);

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }

1 个答案:

答案 0 :(得分:1)

我会尝试这样的事情:

// Parsing json
for (int i = 0; i < response.length(); i++) {
   try {
   JSONObject obj = response.getJSONObject(i);
   if (obj.getString("tipe").equals("tipe1){
        Movie movie = new Movie();
        movie.setTitle(obj.getString("name"));                                movie.setThumbnailUrl(obj.getString("images1"));

        movie.setYear(obj.getInt("id"));
        movie.setTipe(obj.getString("tipe"));    
        // adding movie to movies array
        movieList.add(movie);
   }

   } catch (JSONException e) {
       e.printStackTrace();
   }

}