F_X F_X - 7 months ago 25
Java Question

how to get only specific data we want from json

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"....

this my code in 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();
}

}

Answer

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

}