List of usage examples for org.json.simple.parser JSONParser parse
public Object parse(Reader in) throws IOException, ParseException
From source file:com.nubits.nubot.pricefeeds.feedservices.BitfinexPriceFeed.java
@Override public LastPrice getLastPrice(CurrencyPair pair) { long now = System.currentTimeMillis(); long diff = now - lastRequest; if (diff >= refreshMinTime) { String url = "https://api.bitfinex.com/v1/pubticker/btcusd"; String htmlString;/*from w ww .ja v a 2 s. c o m*/ 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)); double last = Double.valueOf((String) httpAnswerJson.get("last_price")); //Make the average between buy and sell last = Utils.round(last, 8); 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:capabilities.Browser.java
@Override public String dbSearch(String userAgent) throws Exception { String urlInicio, urlCapacidades, urlFim, urlPath; String capacidades = "mobile_browser_version%0D%0A" + "mobile_browser"; // Montagem URL de acesso ao Introspector Servlet WURFL urlPath = "http://localhost:8080/AdapterAPI/"; // Caminho do projeto urlInicio = "introspector.do?action=Form&form=pippo&ua=" + userAgent; urlCapacidades = "&capabilities=" + capacidades; urlFim = "&wurflEngineTarget=performance&wurflUserAgentPriority=OverrideSideloadedBrowserUserAgent"; // Conexo com o Servlet DefaultHttpClient httpClient = new DefaultHttpClient(); HttpGet getRequest = new HttpGet(urlPath + urlInicio + urlCapacidades + urlFim); getRequest.addHeader("accept", "application/json"); HttpResponse response = httpClient.execute(getRequest); if (response.getStatusLine().getStatusCode() != 200) { throw new RuntimeException("Failed : HTTP error code : " + response.getStatusLine().getStatusCode()); }/*from w ww .ja v a 2 s. c o m*/ BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent()))); String buffer; String dados = ""; //System.out.println("Output from Server .... \n"); while ((buffer = br.readLine()) != null) { dados += buffer; } //System.out.println("Sada:\n\t" + dados); httpClient.getConnectionManager().shutdown(); JSONObject my_obj; JSONParser parser = new JSONParser(); my_obj = (JSONObject) parser.parse(dados); JSONObject capabilities = (JSONObject) my_obj.get("capabilities"); return capabilities.toJSONString(); }
From source file:com.nubits.nubot.pricefeeds.BitstampEURPriceFeed.java
@Override public LastPrice getLastPrice(CurrencyPair pair) { long now = System.currentTimeMillis(); long diff = now - lastRequest; if (diff >= refreshMinTime) { String url = "https://www.bitstamp.net/api/eur_usd/"; String htmlString;//from w w w . 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)); double buy = Double.valueOf((String) httpAnswerJson.get("buy")); double sell = Double.valueOf((String) httpAnswerJson.get("sell")); //Make the average between buy and sell double last = Utils.round((buy + sell) / 2, 8); 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.nubits.nubot.pricefeeds.feedservices.BitstampPriceFeed.java
@Override public LastPrice getLastPrice(CurrencyPair pair) { long now = System.currentTimeMillis(); long diff = now - lastRequest; if (diff >= refreshMinTime) { String url = "https://www.bitstamp.net/api/ticker/"; String htmlString;/*from w w w .j a va2 s . c o m*/ 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)); double last = Double.valueOf((String) httpAnswerJson.get("last")); //Make the average between buy and sell last = Utils.round(last, 8); 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.nubits.nubot.pricefeeds.feedservices.CoinmarketcapnorthpolePriceFeed.java
@Override public LastPrice getLastPrice(CurrencyPair pair) { long now = System.currentTimeMillis(); long diff = now - lastRequest; if (diff >= refreshMinTime) { String htmlString;//from w w w. j av a 2s.c o m 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)); double last = Utils.getDouble(httpAnswerJson.get("price")); 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 @Consumes("application/json") @Path("{id}") public void alteraContato(@PathParam("id") int codigo, String content) throws ParseException { JSONObject jsonO;/*from w w w . jav a2 s .co m*/ 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(codigo, contato); }
From source file:com.releasequeue.jenkins.action.ReleaseQueueWebHookAction.java
public void doIndex(StaplerRequest req, StaplerResponse resp) throws Exception { List<Ancestor> ancestors = req.getAncestors(); AbstractProject project = (AbstractProject) ancestors.get(ancestors.size() - 2).getObject(); byte[] data = new byte[req.getContentLength()]; ServletInputStream inputStream = req.getInputStream(); inputStream.read(data);//from w w w . j a v a 2 s. co m JSONParser parser = new JSONParser(); JSONObject jsonObj = JSONObject.fromObject(parser.parse(new String(data))); List<ParameterValue> values = new ArrayList<ParameterValue>(); ParametersDefinitionProperty property = (ParametersDefinitionProperty) project .getProperty(ParametersDefinitionProperty.class); if (property != null) { for (ParameterDefinition parameterDefinition : property.getParameterDefinitions()) { String parameterName = parameterDefinition.getName(); ParameterValue parameterValue = null; if (jsonObj.containsKey(parameterName)) { String strValue = jsonObj.get(parameterName).toString(); if (parameterDefinition.getClass() == StringParameterDefinition.class) { parameterValue = ((StringParameterDefinition) parameterDefinition).createValue(strValue); } else if (parameterDefinition.getClass() == TextParameterDefinition.class) { parameterValue = ((TextParameterDefinition) parameterDefinition).createValue(strValue); } else if (parameterDefinition.getClass() == ChoiceParameterDefinition.class) { if (((ChoiceParameterDefinition) parameterDefinition).getChoices().contains(strValue)) { parameterValue = ((ChoiceParameterDefinition) parameterDefinition) .createValue(strValue); } } } if (parameterValue == null) { parameterValue = parameterDefinition.getDefaultParameterValue(); } values.add(parameterValue); } } Jenkins.getInstance().getQueue().schedule(project, 0, new ParametersAction(values), new CauseAction(new ReleaseQueueBuildCause())); }
From source file:at.yawk.selenium.resourcepack.McMeta.java
public JSONObject getRoot() throws McMetaException { if (rootObject == null) { if (file.exists()) { JSONParser parser = new JSONParser(); try (InputStream i = file.getInput()) { rootObject = (JSONObject) parser.parse(new InputStreamReader(i)); } catch (ParseException | IOException e) { throw new McMetaException(e); }/*from w w w. j a va2s . co m*/ } else { rootObject = new JSONObject(); } } return rootObject; }
From source file:com.nubits.nubot.pricefeeds.CoinmarketcapnexuistPriceFeed.java
@Override public LastPrice getLastPrice(CurrencyPair pair) { long now = System.currentTimeMillis(); long diff = now - lastRequest; if (diff >= refreshMinTime) { String htmlString;//from www. ja v a 2 s. c o m try { htmlString = Utils.getHTML(getUrl(pair), 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 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.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.BitstampEURPriceFeed.java
@Override public LastPrice getLastPrice(CurrencyPair pair) { long now = System.currentTimeMillis(); long diff = now - lastRequest; if (diff >= refreshMinTime) { String url = "https://www.bitstamp.net/api/eur_usd/"; String htmlString;/* w w w.java 2 s. c o m*/ 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)); double buy = Double.valueOf((String) httpAnswerJson.get("buy")); double sell = Double.valueOf((String) httpAnswerJson.get("sell")); //Make the average between buy and sell double last = Utils.round((buy + sell) / 2, 8); 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; } }