List of usage examples for org.json.simple.parser JSONParser parse
public Object parse(Reader in) throws IOException, ParseException
From source file:org.kitodo.data.elasticsearch.index.type.RulesetTypeTest.java
@Test public void shouldCreateDocument() throws Exception { RulesetType rulesetType = new RulesetType(); Ruleset ruleset = prepareData().get(0); JSONParser parser = new JSONParser(); HttpEntity document = rulesetType.createDocument(ruleset); JSONObject actual = (JSONObject) parser.parse(EntityUtils.toString(document)); JSONObject excepted = (JSONObject) parser.parse( "{\"title\":\"SLUBDD\",\"file\":\"ruleset_slubdd.xml\",\"orderMetadataByRuleset\":false\"fileContent\":\"\"}"); assertEquals("Ruleset JSONObject doesn't match to given JSONObject!", excepted, actual); }
From source file:com.nubits.nubot.pricefeeds.BtcePriceFeed.java
@Override public LastPrice getLastPrice(CurrencyPair pair) { long now = System.currentTimeMillis(); long diff = now - lastRequest; if (diff >= refreshMinTime) { String url = getUrl(pair); String htmlString;// ww w . j av a2 s . co m try { htmlString = Utils.getHTML(url, true); } catch (IOException ex) { LOG.severe(ex.toString()); return new LastPrice(true, name, pair.getOrderCurrency(), null); } JSONParser parser = new JSONParser(); try { JSONObject httpAnswerJson = (JSONObject) (parser.parse(htmlString)); JSONObject tickerObject = (JSONObject) httpAnswerJson.get("ticker"); double last = Utils.getDouble(tickerObject.get("last")); lastRequest = System.currentTimeMillis(); lastPrice = new LastPrice(false, name, pair.getOrderCurrency(), new Amount(last, pair.getPaymentCurrency())); return lastPrice; } catch (Exception ex) { LOG.severe(htmlString); LOG.severe(ex.toString()); lastRequest = System.currentTimeMillis(); return new LastPrice(true, name, pair.getOrderCurrency(), null); } } else { LOG.fine("Wait " + (refreshMinTime - (System.currentTimeMillis() - lastRequest)) + " ms " + "before making a new request. Now returning the last saved price\n\n"); return lastPrice; } }
From source file:com.nubits.nubot.pricefeeds.feedservices.CoinmarketcapnexuistPriceFeed.java
@Override public LastPrice getLastPrice(CurrencyPair pair) { long now = System.currentTimeMillis(); long diff = now - lastRequest; if (diff >= refreshMinTime) { String htmlString;// ww w .ja v a2s. c om try { htmlString = Utils.getHTML(getUrl(pair), true); } catch (IOException ex) { LOG.error(ex.toString()); return new LastPrice(true, name, pair.getOrderCurrency(), null); } JSONParser parser = new JSONParser(); try { JSONObject httpAnswerJson = (JSONObject) (parser.parse(htmlString)); JSONObject price = (JSONObject) httpAnswerJson.get("price"); double last = Utils.getDouble(price.get("usd")); lastRequest = System.currentTimeMillis(); lastPrice = new LastPrice(false, name, pair.getOrderCurrency(), new Amount(last, pair.getPaymentCurrency())); return lastPrice; } catch (Exception ex) { LOG.error(ex.toString()); lastRequest = System.currentTimeMillis(); return new LastPrice(true, name, pair.getOrderCurrency(), null); } } else { LOG.warn("Wait " + (refreshMinTime - (System.currentTimeMillis() - lastRequest)) + " ms " + "before making a new request. Now returning the last saved price\n\n"); return lastPrice; } }
From source file:com.praticasolucoes.webserver.recurso.AgendaResource.java
/** * PUT method for updating or creating an instance of AgendaResource * @param content representation for the resource * @return an HTTP response with content of the updated or created resource. *///from www. j a v a 2 s . c o m @PUT @Consumes("application/json") public void putXml(String content) throws ParseException { JSONObject jsonO; JSONObject json1; JSONParser jsonP = new JSONParser(); jsonO = (JSONObject) jsonP.parse(content); json1 = (JSONObject) jsonO.get("operadora"); Contato contato = new Contato(); contato.setNome((String) jsonO.get("nome")); contato.setCodigo(sequencia); contato.setTelefone((String) jsonO.get("telefone")); contato.setOperadora(new Operadora(((Long) json1.get("codigo")).intValue(), (String) json1.get("nome"), (String) json1.get("categoria"))); l.put(contato.getCodigo(), contato); sequencia++; }
From source file:com.stackmob.example.geopoints.WriteGeo.java
@Override public ResponseToProcess execute(ProcessedAPIRequest request, SDKServiceProvider serviceProvider) { LoggerService logger = serviceProvider.getLoggerService(WriteGeo.class); Map<String, SMObject> feedback = new HashMap<String, SMObject>(); Map<String, String> errMap = new HashMap<String, String>(); String user = ""; String latitude = ""; String longitude = ""; JSONParser parser = new JSONParser(); try {//from ww w . j a v a2 s. com Object obj = parser.parse(request.getBody()); JSONObject jsonObject = (JSONObject) obj; user = (String) jsonObject.get("user_name"); latitude = (String) jsonObject.get("Latitude"); longitude = (String) jsonObject.get("Longitude"); } catch (ParseException pe) { logger.error(pe.getMessage(), pe); return Util.badRequestResponse(errMap, pe.getMessage()); } if (Util.hasNulls(user, latitude, longitude)) { return Util.badRequestResponse(errMap, "Please fill in all parameters correctly"); } DataService ds = serviceProvider.getDataService(); List<SMUpdate> update = new ArrayList<SMUpdate>(); Map<String, SMValue> geoPoint = new HashMap<String, SMValue>(); SMObject result; try { geoPoint.put("lat", new SMDouble(Double.parseDouble(latitude))); geoPoint.put("lon", new SMDouble(Double.parseDouble(longitude))); update.add(new SMSet("position", new SMObject(geoPoint))); } catch (NumberFormatException nfe) { logger.error(nfe.getMessage(), nfe); return Util.badRequestResponse(errMap, nfe.getMessage()); } try { result = ds.updateObject("user", new SMString(user), update); feedback.put("Updated object", result); } catch (InvalidSchemaException ise) { return Util.internalErrorResponse("invalid_schema", ise, errMap); // http 500 - internal server error } catch (DatastoreException dse) { return Util.internalErrorResponse("datastore_exception", dse, errMap); // http 500 - internal server error } return new ResponseToProcess(HttpURLConnection.HTTP_OK, feedback); }
From source file:com.nubits.nubot.pricefeeds.feedservices.BtcePriceFeed.java
@Override public LastPrice getLastPrice(CurrencyPair pair) { long now = System.currentTimeMillis(); long diff = now - lastRequest; if (diff >= refreshMinTime) { String url = getUrl(pair); String htmlString;/*from w ww . ja v a 2 s.c om*/ try { htmlString = Utils.getHTML(url, true); } catch (IOException ex) { LOG.error(ex.toString()); return new LastPrice(true, name, pair.getOrderCurrency(), null); } JSONParser parser = new JSONParser(); try { JSONObject httpAnswerJson = (JSONObject) (parser.parse(htmlString)); JSONObject tickerObject = (JSONObject) httpAnswerJson.get("ticker"); double last = Utils.getDouble(tickerObject.get("last")); lastRequest = System.currentTimeMillis(); lastPrice = new LastPrice(false, name, pair.getOrderCurrency(), new Amount(last, pair.getPaymentCurrency())); return lastPrice; } catch (Exception ex) { LOG.error(htmlString); LOG.error(ex.toString()); lastRequest = System.currentTimeMillis(); return new LastPrice(true, name, pair.getOrderCurrency(), null); } } else { LOG.warn("Wait " + (refreshMinTime - (System.currentTimeMillis() - lastRequest)) + " ms " + "before making a new request. Now returning the last saved price\n\n"); return lastPrice; } }
From source file:StudentsUpdate.java
private void getStud(String result) { System.out.println("result ==>" + result); try {// ww w. j a v a 2 s . c o m JSONParser parser = new JSONParser(); JSONObject mainjsonObj = (JSONObject) parser.parse(result); JSONArray jsonar = (JSONArray) mainjsonObj.get("students"); for (int i = 0; i < jsonar.size(); i++) { JSONObject c = (JSONObject) jsonar.get(i); // System.out.println(c.get("pa")); // stud.add("sk"); // pa.add((String)c.get("pa")); } } catch (Exception e) { e.printStackTrace(); } }
From source file:ElasticSearchConnection.java
public void putMappings(String fileName, String type) { JSONParser parser = new JSONParser(); try {//from w w w.ja v a2 s .co m Object obj = parser .parse(new InputStreamReader(ElasticSearchConnection.class.getResourceAsStream(fileName))); JSONObject jsonObject = (JSONObject) obj; PutMappingResponse response = getClient().admin().indices().preparePutMapping(getIndex()).setType(type) .setSource(jsonObject.toJSONString()).execute().actionGet(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } }
From source file:com.nubits.nubot.pricefeeds.YahooPriceFeed.java
@Override public LastPrice getLastPrice(CurrencyPair pair) { long now = System.currentTimeMillis(); long diff = now - lastRequest; if (diff >= refreshMinTime) { String url = getUrl(pair); String htmlString;/* www . ja v a2 s. com*/ try { htmlString = Utils.getHTML(url, true); } catch (IOException ex) { LOG.severe(ex.toString()); return new LastPrice(true, name, pair.getOrderCurrency(), null); } JSONParser parser = new JSONParser(); try { JSONObject httpAnswerJson = (JSONObject) (parser.parse(htmlString)); JSONObject query = (JSONObject) httpAnswerJson.get("query"); JSONObject results = (JSONObject) query.get("results"); JSONObject rate = (JSONObject) results.get("rate"); double last = Utils.getDouble((String) rate.get("Rate")); lastRequest = System.currentTimeMillis(); lastPrice = new LastPrice(false, name, pair.getOrderCurrency(), new Amount(last, pair.getPaymentCurrency())); return lastPrice; } catch (Exception ex) { LOG.severe(ex.toString()); lastRequest = System.currentTimeMillis(); return new LastPrice(true, name, pair.getOrderCurrency(), null); } } else { LOG.fine("Wait " + (refreshMinTime - (System.currentTimeMillis() - lastRequest)) + " ms " + "before making a new request. Now returning the last saved price\n\n"); return lastPrice; } }
From source file:com.rogue.reginald.config.ConfigurationLoader.java
private void loadConfig() throws IOException, ParseException { if (!this.config.exists()) { this.saveResource("config.json"); }// www.j a v a 2 s .c om System.out.println(this.config.getAbsolutePath()); JSONParser parser = new JSONParser(); this.root = (JSONObject) parser.parse(new FileReader(this.config)); for (ConfigValue val : ConfigValue.values()) { if (!this.isSet(val)) { this.set(val, val.getDefault()); } } }