Filter json city returned from google places

0

I need an example to understand how I can go through a jsonArray. This is the example I'm using

link

/** Receives a JSONObject and returns a list */
public List<HashMap<String,String>> parse(JSONObject jObject){      

    JSONArray jPlaces = null;
    JSONArray jPlacesAll = null;
    try {           
        /** Retrieves all the elements in the 'places' array */
        jPlaces = jObject.getJSONArray("predictions");

    } catch (JSONException e) {
        e.printStackTrace();
    }
    /** Invoking getPlaces with the array of json object
     * where each json object represent a place
     */     
    return getPlaces(jPlaces);
}

My class PlaceJSONParser {} takes all json elements and puts them in an array, but I need to check the terms [] of each to filter the city and then put it in the array. I could not access the terms [] can you give me a force ??

The complete class:

public class PlaceJSONParser {

    /** Receives a JSONObject and returns a list */
    public List<HashMap<String,String>> parse(JSONObject jObject){      

        JSONArray jPlaces = null;
        JSONArray jPlacesAll = null;
        try {           
            /** Retrieves all the elements in the 'places' array */
            jPlaces = jObject.getJSONArray("predictions");

        } catch (JSONException e) {
            e.printStackTrace();
        }
        /** Invoking getPlaces with the array of json object
         * where each json object represent a place
         */     
        return getPlaces(jPlaces);
    }


    private List<HashMap<String, String>> getPlaces(JSONArray jPlaces){
        int placesCount = jPlaces.length();
        List<HashMap<String, String>> placesList = new ArrayList<HashMap<String,String>>();
        HashMap<String, String> place = null;
        HashMap<String, String> terms = null;
        List<HashMap<String, String>> termsList = new ArrayList<HashMap<String,String>>();

        /** Taking each place, parses and adds to list object */
        for(int i=0; i<placesCount;i++){
            try {
                /** Call getPlace with place JSON object to parse the place */
                place = getPlace((JSONObject)jPlaces.get(i));               

                placesList.add(place);


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

        return placesList;
    }

    /** Parsing the Place JSON object */
    private HashMap<String, String> getPlace(JSONObject jPlace){

        HashMap<String, String> place = new HashMap<String, String>();

        String id="";
        String reference="";
        String description="";      
        String terms= null;



        try {

                description = jPlace.getString("description");          
                id = jPlace.getString("id");
                reference = jPlace.getString("reference");

                /** 
                 * Formata o endereço para o autocomplete 
                 * */
                String[] parts = description.split("-");
                String part1 =parts[0]; // endereço , bairro            
                description = part1;            

                /** Acrescenta os endereços */
                place.put("description", description);
                place.put("_id",id);
                place.put("reference",reference);                                       

        } catch (JSONException e) {         
            e.printStackTrace();
        }       
        return place;
    }
}
    
asked by anonymous 12.01.2015 / 15:28

1 answer

1

In the getPlace function you can use

JSONArray terms = jPlace.getJSONArray("terms")

To access the terms terms.

    
12.01.2015 / 16:01