List of usage examples for com.google.gson JsonObject get
public JsonElement get(String memberName)
From source file:com.adobe.ags.curly.controller.DataImporterController.java
License:Apache License
private void openJson(File file) throws FileNotFoundException, IOException { FileReader fileReader = new FileReader(file); try (BufferedReader reader = new BufferedReader(fileReader)) { JsonParser parser = new JsonParser(); JsonObject data = parser.parse(reader).getAsJsonObject(); data.entrySet().stream().filter((entry) -> (entry.getValue().isJsonArray())).forEach((entry) -> { worksheetSelector.getItems().add(entry.getKey()); });//from w w w . java2 s. co m sheetReader = (String node) -> readNodes(data.get(node).getAsJsonArray()); skipFirstSelection.setValue(1); Platform.runLater(() -> worksheetSelector.getSelectionModel().selectFirst()); } }
From source file:com.adobe.ags.curly.controller.DataImporterController.java
License:Apache License
private List<List<String>> readNodes(JsonArray data) { TreeSet<String> attributes = new TreeSet<>(); List<Map<String, String>> rows = new ArrayList<>(); data.forEach((elem) -> {/*from www .j a v a 2s. co m*/ if (elem.isJsonObject()) { JsonObject row = elem.getAsJsonObject(); Map<String, String> rowMap = row.entrySet().stream() .filter((entry) -> entry.getValue().isJsonPrimitive()).collect(Collectors .toMap((entry) -> entry.getKey(), (entry) -> entry.getValue().getAsString())); rows.add(rowMap); attributes.addAll(rowMap.keySet()); } }); List<List<String>> results = rows.stream() .map((row) -> attributes.stream().map((attr) -> row.get(attr)).collect(Collectors.toList())) .collect(Collectors.toList()); results.add(0, new ArrayList<String>(attributes)); return results; }
From source file:com.adssets.api.Scout24.java
@Override public String getApartments(String objectId) { try {// w w w . j av a 2s . c o m // Make an API call... it is required to sign each request (see below) //1276 // URL url = new URL("http://rest.immobilienscout24.de/restapi/api/search/v1.0/search/region?realestatetype=apartmentbuy&geocodes=" + regionid ); URL url = new URL("https://rest.immobilienscout24.de/restapi/api/search/v1.0/expose/" + objectId); HttpURLConnection requestUrl = null; requestUrl = (HttpURLConnection) url.openConnection(); consumer.sign(requestUrl); // System.out.println("Sending request..."); requestUrl.connect(); // System.out.println("Expiration " + requestUrl.getExpiration()); // // System.out.println("Timeout " + requestUrl.getConnectTimeout()); // // System.out.println("URL " + requestUrl.getURL()); // // System.out.println("Method " + requestUrl.getRequestMethod()); // // System.out.println("Response: " + requestUrl.getResponseCode() + " " + requestUrl.getResponseMessage()); BufferedReader br = new BufferedReader(new InputStreamReader(requestUrl.getInputStream())); StringBuilder sb = new StringBuilder(); String line; switch (requestUrl.getResponseCode()) { case 200: while ((line = br.readLine()) != null) { sb.append(line + "\n"); } br.close(); try { JSONObject outObj = XML.toJSONObject(sb.toString()); JsonParser parser = new JsonParser(); Object objPrice = new Object(); JsonObject objPriceJson = new JsonObject(); // JSONObject objTitlePicture = new JSONObject(); JSONArray objAllPictures = new JSONArray(); JSONObject objAdPicture = new JSONObject(); JSONObject objAddress = new JSONObject(); Object objPricem2 = new Object(); JsonObject objPricem2Json = new JsonObject(); JSONObject objLivingspace = new JSONObject(); JSONObject obj = new JSONObject(); // System.out.println(outObj.toString()); objPrice = outObj.getJSONObject("expose:expose").getJSONObject("realEstate") .get("calculatedPrice"); //price objPriceJson = parser.parse(objPrice.toString()).getAsJsonObject(); // objTitlePicture = outObj.getJSONObject("expose:expose").getJSONObject("realEstate").getJSONObject("titlePicture").getJSONObject("urls") // .getJSONArray("url").getJSONObject(1); objAllPictures = outObj.getJSONObject("expose:expose").getJSONObject("realEstate") .getJSONObject("attachments").getJSONArray("attachment"); objAdPicture = outObj.getJSONObject("expose:expose").getJSONObject("realEstate") .getJSONObject("titlePicture").getJSONObject("urls").getJSONArray("url") .getJSONObject(4); objAddress = outObj.getJSONObject("expose:expose").getJSONObject("realEstate") .getJSONObject("address"); objPricem2 = outObj.getJSONObject("expose:expose").getJSONObject("realEstate").get("price"); objPricem2Json = parser.parse(objPricem2.toString()).getAsJsonObject(); obj.put("price", objPrice); obj.put("allpictures", objAllPictures); obj.put("adpicture", objAdPicture); obj.put("address", objAddress); obj.put("livingspace", (int) Math.round((Double.parseDouble(objPriceJson.get("value").getAsString()) / Double.parseDouble(objPricem2Json.get("value").getAsString())))); return obj.toString(); //obj } catch (JSONException je) { je.printStackTrace(); return "{\"error\":2}"; } case 201: while ((line = br.readLine()) != null) { sb.append(line + "\n"); } br.close(); try { JSONObject outObj = XML.toJSONObject(sb.toString()); return outObj.toString(); } catch (JSONException je) { je.printStackTrace(); return "{\"error\":2}"; } } } catch (Exception e) { System.out.println("-------REMOVE OBJECT------"); e.printStackTrace(); return "{\"error\":1}"; } return "{\"error\":1}"; }
From source file:com.adssets.api.Scout24.java
@Override public String getLoans(String postalcode, String suburb) { try {//from w w w.ja v a2 s .c om // Make an API call... it is required to sign each request (see below) Boolean getGeoCode = false; URL url; if (postalcode != null) { url = new URL( "http://rest.immobilienscout24.de/restapi/api/financing/construction/v2/offer?postalcode=" + postalcode); } else if (suburb != null) { getGeoCode = true; suburb = suburb.replace("_", "*"); url = new URL("https://rest.immobilienscout24.de/restapi/api/search/v1.0/region?q=" + suburb); } else { url = new URL("http://rest.immobilienscout24.de/restapi/api/financing/construction/v2/offer"); } HttpURLConnection requestUrl = null; requestUrl = (HttpURLConnection) url.openConnection(); consumer.sign(requestUrl); requestUrl.connect(); BufferedReader br = new BufferedReader(new InputStreamReader(requestUrl.getInputStream())); StringBuilder sb = new StringBuilder(); String line; switch (requestUrl.getResponseCode()) { case 200: while ((line = br.readLine()) != null) { sb.append(line + "\n"); } br.close(); try { JsonParser parser = new JsonParser(); JsonObject outObj = parser.parse(sb.toString()).getAsJsonObject(); //If a suburb is sent, get the geocode for that suburb if (getGeoCode) { System.out.println("GETGEOCODE"); //Get geoCodeId System.out.println(outObj.toString()); String geoCodeId = outObj.get("region.regions").getAsJsonArray().get(0).getAsJsonObject() .get("region").getAsJsonArray().get(0).getAsJsonObject().get("geoCodeId") .getAsString(); System.out.println(geoCodeId); //1276003001003 1276003001 url = new URL( "http://rest.immobilienscout24.de/restapi/api/financing/construction/v2/offer?geocode=" + geoCodeId); requestUrl = null; requestUrl = (HttpURLConnection) url.openConnection(); consumer.sign(requestUrl); requestUrl.connect(); br = new BufferedReader(new InputStreamReader(requestUrl.getInputStream())); sb = new StringBuilder(); while ((line = br.readLine()) != null) { sb.append(line + "\n"); } outObj = parser.parse(sb.toString()).getAsJsonObject(); return sortData(outObj); } else { return sortData(outObj); } } catch (JSONException je) { je.printStackTrace(); return "{\"error\":2}"; } case 201: while ((line = br.readLine()) != null) { sb.append(line + "\n"); } br.close(); try { JSONObject outObj = XML.toJSONObject(sb.toString()); return outObj.toString(); } catch (JSONException je) { je.printStackTrace(); return "{\"error\":2}"; } } } catch (Exception e) { e.printStackTrace(); } return "{\"error\":1}"; }
From source file:com.adssets.api.Scout24.java
public String sortData(JsonObject outObj) { List<JsonObject> jsonValues = new ArrayList<>(); for (int i = 0; i < outObj.get("offer").getAsJsonArray().size(); i++) { if (outObj.get("offer").getAsJsonArray().get(i).getAsJsonObject().has("amortizationData")) { JsonObject obj = new JsonObject(); obj.add("amortizationData", outObj.get("offer").getAsJsonArray().get(i).getAsJsonObject().get("amortizationData")); obj.add("distanceToSearchLocation", outObj.get("offer").getAsJsonArray().get(i).getAsJsonObject() .get("distanceToSearchLocation")); obj.add("logo", outObj.get("offer").getAsJsonArray().get(i).getAsJsonObject() .get("mortgageProvider").getAsJsonObject().get("logo")); obj.add("landingPageUrl", outObj.get("offer").getAsJsonArray().get(i).getAsJsonObject() .get("mortgageProvider").getAsJsonObject().get("landingPageUrl")); obj.add("footnote", outObj.get("offer").getAsJsonArray().get(i).getAsJsonObject().get("footnote")); jsonValues.add(obj);/* w w w .java2s .c om*/ } } Collections.sort(jsonValues, new Comparator<JsonObject>() { private static final String KEY_NAME = "distanceToSearchLocation"; @Override public int compare(JsonObject a, JsonObject b) { int valA; int valB; try { if (!a.get(KEY_NAME).isJsonNull() && !b.get(KEY_NAME).isJsonNull()) { valA = (Integer) a.get(KEY_NAME).getAsInt(); valB = (Integer) b.get(KEY_NAME).getAsInt(); return valA - valB; } else { return -1; } } catch (Exception e) { System.err.println(a); System.err.println(b); System.err.println(e); } return -1; } }); return jsonValues.toString(); }
From source file:com.adssets.ejb.DataAccess.java
@Override public String createMarket(String obj) { JsonObject jsonObject = (new JsonParser()).parse(obj).getAsJsonObject(); Market market = new Market(); market.setName(jsonObject.get("name").getAsString()); market.setDescription(jsonObject.get("description").getAsString()); marketFacade.create(market);/*from w w w .j a v a 2 s .c o m*/ JsonArray jsonArray = new JsonArray(); JsonObject jsonObj = new JsonObject(); jsonObj.addProperty("id", market.getId()); jsonObj.addProperty("name", market.getName()); jsonObj.addProperty("description", market.getDescription()); jsonArray.add(jsonObj); return jsonArray.toString(); }
From source file:com.adssets.ejb.DataAccess.java
@Override public String createFeed(String obj) { JsonObject jsonObject = (new JsonParser()).parse(obj).getAsJsonObject(); Market marketId = marketFacade.find(jsonObject.get("marketId").getAsInt()); // Feed feedCheck = feedFacade.find(marketId); List<Feed> feeds = em.createNamedQuery("Feed.findByMarketId").setParameter("marketId", marketId) .getResultList();// www. jav a 2s. c om Integer feedCheck = null; if (feeds.size() > 0) { feedCheck = feeds.get(0).getId(); } Feed feed = new Feed(); feed.setIdmarket(marketId); feed.setJson(jsonObject.get("json").getAsJsonArray().toString()); JsonArray jsonArray = new JsonArray(); JsonObject jsonObj = new JsonObject(); if (feedCheck == null) { feedFacade.create(feed); jsonObj.addProperty("id", feed.getId()); jsonObj.addProperty("marketId", feed.getIdmarket().getId()); jsonObj.addProperty("json", feed.getJson()); jsonArray.add(jsonObj); return jsonArray.toString(); } else { feed.setId(feedCheck); feedFacade.edit(feed); jsonObj.addProperty("id", feed.getId()); jsonObj.addProperty("marketId", feed.getIdmarket().getId()); jsonObj.addProperty("json", feed.getJson()); jsonArray.add(jsonObj); return jsonArray.toString(); } }
From source file:com.adssets.ejb.DataAccess.java
@Override public String buildFeedForMarket(String marketId, String lazyLoad) { JsonArray jsonArray = new JsonArray(); JsonParser parser = new JsonParser(); // Feed feed = feedFacade.find(marketId); Market market = marketFacade.find(Integer.valueOf(marketId)); List<Feed> feeds = em.createNamedQuery("Feed.findByMarketId").setParameter("marketId", market) .getResultList();/*w w w.ja v a2 s . c om*/ if (feeds.size() > 0) { for (Feed feed : feeds) { JsonObject obj = new JsonObject(); obj.addProperty("id", feed.getId()); obj.addProperty("marketId", feed.getIdmarket().getId()); obj.addProperty("json", feed.getJson()); jsonArray.add(obj); } JsonObject obj = jsonArray.get(0).getAsJsonObject(); String objArray = obj.get("json").getAsString(); JsonArray objArrayParsed = parser.parse(objArray).getAsJsonArray(); JsonArray objData = new JsonArray(); for (JsonElement objId : objArrayParsed) { JsonObject objElmParsed = parser.parse(objId.toString()).getAsJsonObject(); System.out.println(objElmParsed); // try{ // isInteger(objId.getAsString()); String value = objElmParsed.get("type").getAsString(); System.out.println(value); //CHANGE ADPICTURE TO THE PICTURE STORED IN THE DATABASE if (value.equals("object")) { String result = scout24.getApartments(objElmParsed.get("objectid").getAsString()); if (!result.equals("{\"error\":1}")) { JsonObject resultObj = parser.parse(result).getAsJsonObject(); String link = "{\"link\": \"https://www.immobilienscout24.de/expose/" + objElmParsed.get("objectid").getAsInt() + "?referrer=\"}"; JsonObject clickLink = parser.parse(link).getAsJsonObject(); resultObj.add("clickLink", clickLink); resultObj.add("objectId", objElmParsed.get("objectid")); resultObj.add("marketId", parser.parse(marketId)); // Use the selected image as ad image JsonObject objHref = new JsonObject(); objHref.add("href", objElmParsed.get("url")); resultObj.add("adpicture", objHref); //If lazyLoad = yes dont return "allpictures" LazyLoad=yes is the parameter the ad will send so it does not get unneccesary data if (lazyLoad.equals("yes")) { resultObj.remove("allpictures"); } objData.add(resultObj); } } else { objData.add(objId); } // }catch(UnsupportedOperationException ex){ // objData.add(objId); // } } return objData.toString(); } else { return new JsonArray().toString(); } }
From source file:com.adyen.deserializer.MarketPayNotificationMessageDeserializer.java
License:MIT License
@Override public GenericNotification deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { JsonObject jsonObject = jsonElement.getAsJsonObject(); JsonElement jsonType = jsonObject.get("eventType"); String eventType = jsonType.getAsString(); if (GenericNotification.EventTypeEnum.ACCOUNT_CREATED.toString().equalsIgnoreCase(eventType)) { return jsonDeserializationContext.deserialize(jsonElement, AccountCreatedNotification.class); }/*from w w w . j a v a 2 s .c o m*/ if (GenericNotification.EventTypeEnum.ACCOUNT_HOLDER_CREATED.toString().equalsIgnoreCase(eventType)) { return jsonDeserializationContext.deserialize(jsonElement, AccountHolderCreatedNotification.class); } if (GenericNotification.EventTypeEnum.ACCOUNT_HOLDER_LIMIT_REACHED.toString().equalsIgnoreCase(eventType)) { return jsonDeserializationContext.deserialize(jsonElement, AccountHolderLimitReachedNotification.class); } if (GenericNotification.EventTypeEnum.ACCOUNT_HOLDER_VERIFICATION.toString().equalsIgnoreCase(eventType)) { return jsonDeserializationContext.deserialize(jsonElement, AccountHolderVerificationNotification.class); } if (GenericNotification.EventTypeEnum.ACCOUNT_HOLDER_STATUS_CHANGE.toString().equalsIgnoreCase(eventType)) { return jsonDeserializationContext.deserialize(jsonElement, AccountHolderStatusChangeNotification.class); } if (GenericNotification.EventTypeEnum.ACCOUNT_HOLDER_PAYOUT.toString().equalsIgnoreCase(eventType)) { return jsonDeserializationContext.deserialize(jsonElement, AccountHolderPayoutNotification.class); } if (GenericNotification.EventTypeEnum.ACCOUNT_HOLDER_UPDATED.toString().equalsIgnoreCase(eventType)) { return jsonDeserializationContext.deserialize(jsonElement, AccountHolderUpdatedNotification.class); } if (GenericNotification.EventTypeEnum.BENEFICIARY_SETUP.toString().equalsIgnoreCase(eventType)) { return jsonDeserializationContext.deserialize(jsonElement, BeneficiarySetupNotification.class); } if (GenericNotification.EventTypeEnum.SCHEDULED_REFUNDS.toString().equalsIgnoreCase(eventType)) { return jsonDeserializationContext.deserialize(jsonElement, ScheduledRefundsNotification.class); } if (GenericNotification.EventTypeEnum.COMPENSATE_NEGATIVE_BALANCE.toString().equalsIgnoreCase(eventType)) { return jsonDeserializationContext.deserialize(jsonElement, CompensateNegativeBalanceNotification.class); } if (GenericNotification.EventTypeEnum.PAYMENT_FAILURE.toString().equalsIgnoreCase(eventType)) { return jsonDeserializationContext.deserialize(jsonElement, PaymentFailureNotification.class); } if (GenericNotification.EventTypeEnum.REPORT_AVAILABLE.toString().equalsIgnoreCase(eventType)) { return jsonDeserializationContext.deserialize(jsonElement, ReportAvailableNotification.class); } if (GenericNotification.EventTypeEnum.TRANSFER_FUNDS.toString().equalsIgnoreCase(eventType)) { return jsonDeserializationContext.deserialize(jsonElement, TransferFundsNotification.class); } return jsonDeserializationContext.deserialize(jsonElement, GenericNotification.class); }
From source file:com.agwego.common.GsonHelper.java
License:Open Source License
/** * Short hand, encapsulate the check if the object has the key and return the appropriate String otherwise null * * @param jObject the JsonObject in question * @param key the key to lookup/*from w ww . j a va2 s. c o m*/ * @param otherwise value to return if no key is found * @return the value for key as a String */ public static String getAsString(final JsonObject jObject, final String key, final String otherwise) { return jObject.has(key) ? jObject.get(key).getAsString() : otherwise; }