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

时间:2022-09-15 16:43:18

i have trouble to load somedata, i already database and has query name "tipe" (type), in query tipe only has 2 type of string ("1" and "2") So, how to load data if im only want load sepecific data tipe="1"....

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

this my code in android :

这是我在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 个解决方案

#1


1  

i would try somethig like this:

我会尝试这样的事情:

// 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();
   }

}

#1


1  

i would try somethig like this:

我会尝试这样的事情:

// 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();
   }

}