All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.googlecode.placesapiclient.client.parser.impl.PlacePredictionParser Maven / Gradle / Ivy

package com.googlecode.placesapiclient.client.parser.impl;

import com.googlecode.placesapiclient.client.entity.PlacePrediction;
import com.googlecode.placesapiclient.client.exception.ErrorCode;
import com.googlecode.placesapiclient.client.exception.ErrorCodeException;
import com.googlecode.placesapiclient.client.parser.JSONParser;
import com.googlecode.placesapiclient.client.util.Converter;
import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 */
public class PlacePredictionParser implements JSONParser {

    private static final Logger logger = Logger.getLogger(PlacePredictionParser.class.getName());

    public static final String PREDICTIONS = "predictions";

    @Override
    @SuppressWarnings("unchecked")
    public List parse(JSONObject jsonObject) throws ErrorCodeException {

        if(!jsonObject.has(PREDICTIONS)) {
            throw new ErrorCodeException(ErrorCode.INVALID_JSON_OBJECT, "Invalid JSONObject element");
        }
        try {
            JSONArray array = jsonObject.getJSONArray(PREDICTIONS);

            List arrayList =new ArrayList();
            for (int i = 0; i < array.length(); i++) {

                PlacePrediction place = Converter.convertJsonToPlacePrediction((JSONObject) array.get(i));
                logger.info(place.toString());
                arrayList.add(place);

            }
            return arrayList;
        } catch (Exception th) {
            logger.error("Error during parsing JSONObject", th);
            throw new ErrorCodeException(ErrorCode.INVALID_JSON_OBJECT, "Invalid JSONObject element");
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy