Example usage for org.json.simple JSONObject entrySet

List of usage examples for org.json.simple JSONObject entrySet

Introduction

In this page you can find the example usage for org.json.simple JSONObject entrySet.

Prototype

Set<Map.Entry<K, V>> entrySet();

Source Link

Document

Returns a Set view of the mappings contained in this map.

Usage

From source file:report.Report.java

/**
 * Initialize the IXDocReport struct with the JSON
 * @param jsonText The JSON text./*ww w  . ja v a2 s.com*/
 * @throws IOException If the template or output file can't be opened or written.
 * @throws GeneratorException 
 */
private void buildFromJson(String jsonText) throws GeneratorException {
    // Parses the JSON text:
    JSONObject json;
    try {
        JSONParser parser = new JSONParser();
        json = (JSONObject) parser.parse(jsonText);
    } catch (ParseException e) {
        throw new GeneratorException(GeneratorError.JSON_ERROR,
                "Error while parsing the JSON file:" + e.toString());
    }

    // Initializes the template file and creates the report object:
    try {
        InputStream in = new FileInputStream(template);
        report = XDocReportRegistry.getRegistry().loadReport(in, TemplateEngineKind.Velocity);
    } catch (XDocReportException e) {
        throw new GeneratorException(GeneratorError.TEMPLATE_ERROR,
                "Error with the template file:" + e.toString());
    } catch (FileNotFoundException e) {
        throw new GeneratorException(GeneratorError.PARAMETER_ERROR, "Template file does not exists.");
    } catch (IOException e) {
        throw new GeneratorException(GeneratorError.IO_ERROR, "I/O error...");
    }

    // Creates the FieldsMetadata.
    metadata = new FieldsMetadata();

    // Creates the report context:
    try {
        context = report.createContext();
    } catch (XDocReportException e) {
        throw new GeneratorException(GeneratorError.CONTEXT_ERROR,
                "Error while creating the context:" + e.toString());
    }

    Iterator<?> iter = json.entrySet().iterator();
    while (iter.hasNext()) {
        Map.Entry<?, ?> entry = (Map.Entry<?, ?>) iter.next();
        BuilderFactory.getBuilder(entry.toString()).build((JSONObject) entry.getValue(), metadata, context);
    }

    // Links the FieldsMetaData to the report.
    report.setFieldsMetadata(metadata);
}

From source file:se.kodapan.geography.geocoding.Nominatim.java

private Result resultFactory(JSONObject jsonResult) {
    Result result = new ResultImpl();
    result.setSource(sourceFactory());//from   w w  w . ja  va 2 s.c  om

    String licence = ((String) jsonResult.remove("licence"));
    result.getSource().setLicense(licence);
    // todo place_id can be a string error message, at least if reverse geocoding out of area
    Number placeIdentity = Long.valueOf(((String) jsonResult.remove("place_id")));
    String placeClass = ((String) jsonResult.remove("class"));
    String placeType = ((String) jsonResult.remove("type"));
    String osmIdentity = ((String) jsonResult.remove("osm_id"));

    String osmType = ((String) jsonResult.remove("osm_type"));

    String iconURL = ((String) jsonResult.remove("icon"));

    String displayName = ((String) jsonResult.remove("display_name"));
    result.getAddressComponents().setFormattedAddress(displayName);

    Number latitude = Double.valueOf((String) jsonResult.remove("lat"));
    Number longitude = Double.valueOf((String) jsonResult.remove("lon"));
    result.setLocation(new CoordinateImpl(latitude, longitude));

    JSONObject address = (JSONObject) jsonResult.remove("address");
    if (address != null) {
        for (Map.Entry<String, String> ae : ((Set<Map.Entry<String, String>>) address.entrySet())) {
            result.getAddressComponents().add(new AddressComponent(ae.getValue(), ae.getKey()));
        }
    }

    JSONArray boundingBox = (JSONArray) jsonResult.remove("boundingbox");
    if (boundingBox != null) {
        result.setBounds(new EnvelopeImpl(
                new CoordinateImpl(Double.valueOf(boundingBox.get(0).toString()),
                        Double.valueOf(boundingBox.get(2).toString())),
                new CoordinateImpl(Double.valueOf(boundingBox.get(1).toString()),
                        Double.valueOf(boundingBox.get(3).toString()))));
    }

    JSONArray polygonPoints = (JSONArray) jsonResult.remove("polygonpoints");
    if (polygonPoints != null) {
        if (result.getBounds() != null) {
            // according to the osm irc this means that the bounding box enclose the polygon
            log.info("Both boundingbox and polygon in response, using the latter");
        }
        List<Coordinate> polygonCoordinates = new ArrayList<Coordinate>(polygonPoints.size());
        for (int ppsi = 0; ppsi < polygonPoints.size(); ppsi++) {
            JSONArray polygonPoint = (JSONArray) polygonPoints.get(ppsi);
            polygonCoordinates.add(new CoordinateImpl(Double.valueOf(polygonPoint.get(1).toString()),
                    Double.valueOf(polygonPoint.get(0).toString())));
        }
        if (!polygonCoordinates.isEmpty()) {
            EnvelopeImpl envelope = new EnvelopeImpl();
            envelope.addBounds(polygonCoordinates);
            result.setBounds(envelope);
        }

    }

    if (!jsonResult.isEmpty()) {
        log.error("Unknown data left in result: " + jsonResult.toJSONString());
    }

    return result;
}

From source file:uk.ac.susx.tag.method51.twitter.geocoding.geonames.GeonamesSPARQLLocationDatabase.java

private List<LocationMatch> parseResponse(String response, LocationCandidate candidate)
        throws ParseException, LocationUnresolvedException {

    JSONParser jsonParser = new JSONParser();

    List<LocationMatch> matches = new LinkedList<>();

    JSONObject root = (JSONObject) jsonParser.parse(response);

    JSONArray bindings = (JSONArray) ((Map) root.get("results")).get("bindings");

    for (Object object1 : bindings) {
        JSONObject binding = (JSONObject) object1;

        Map<String, String> data = new HashMap<>();

        for (Object object2 : binding.entrySet()) {
            Map.Entry field = (Map.Entry) object2;

            String key = (String) field.getKey();
            String value = (String) ((Map) field.getValue()).get("value");

            data.put(key, value);/*  w ww. jav  a  2 s  . com*/
        }

        LocationMatch match = new LocationMatch();

        match.data = data;
        if (candidate != null) {
            match.matchingText = candidate.getCandidateString();
            match.matchBegin = candidate.getBegin();
            match.matchEnd = candidate.getEnd();
        }

        String lat = data.get(LATITUDE_KEY);
        String lon = data.get(LONGITUDE_KEY);

        if (lat == null || lon == null) {
            throw new LocationUnresolvedException("null lat or lon in geonames db entry " + data.get("x"));
        }

        try {
            match.lat = Double.parseDouble(lat);
            match.lon = Double.parseDouble(lon);
        } catch (NumberFormatException e) {
            throw new LocationUnresolvedException("could not parse lat / lon to numbers " + lat + " / " + lon);
        }

        matches.add(match);

        //System.out.println((String)entry.get("x"));
    }

    return matches;
}