Example usage for org.json.simple.parser JSONParser JSONParser

List of usage examples for org.json.simple.parser JSONParser JSONParser

Introduction

In this page you can find the example usage for org.json.simple.parser JSONParser JSONParser.

Prototype

JSONParser

Source Link

Usage

From source file:com.nubits.nubot.pricefeeds.feedservices.BlockchainPriceFeed.java

@Override
public LastPrice getLastPrice(CurrencyPair pair) {
    String url = "https://blockchain.info/ticker";
    long now = System.currentTimeMillis();
    long diff = now - lastRequest;
    if (diff >= refreshMinTime) {
        String htmlString = "";
        try {/*from  ww w.  jav  a 2  s .  c om*/
            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("USD");
            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) {
            lastRequest = System.currentTimeMillis();
            LOG.error(ex.toString());
            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.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;/*  w ww .  j a  v a  2s.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:me.wxwsk8er.Knapsack.Configuration.JSONConfiguration.java

/**
 * Parses /*from  w  w  w .  j  ava 2  s .c om*/
 */
private void parseNew() {
    try {
        this.parser = new JSONParser();
        this.jData = parser.parse(new FileReader(new File(this.getClass().getResource(filePath).toURI())));
        this.jObject = (JSONObject) jData;
    } catch (Exception e) {
        e.printStackTrace();
    }
}

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 www  .j  av  a2  s .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:eu.celarcloud.celar_ms.ServerPack.ServerRegister.java

public void run() {
    if (this.server.inDebugMode())
        System.out.println("\nServerRegister>> processing the following message...\n" + msg[0] + " " + msg[1]
                + "\n" + msg[2]);
    try {//ww w.  j  ava 2s  . c  o  m
        JSONParser parser = new JSONParser();
        JSONObject json;

        json = (JSONObject) parser.parse(msg[2]); //parse content

        if (msg[1].equals("SERVER.CONNECT"))
            connect(json);
        //else if (msg[1].equals("SERVER.REGISTER"))
        //metrics(json);   
        else
            this.response(Status.ERROR, msg[1] + " request does not exist");
    } catch (NullPointerException e) {
        this.server.writeToLog(Level.SEVERE, e);
    } catch (Exception e) {
        this.server.writeToLog(Level.SEVERE, e);
    }
}

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;/*  w ww . j a va 2s.com*/
        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.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;//from ww w  .  java 2  s.co 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;
    }
}

From source file:com.codelanx.codelanxlib.data.types.Json.java

/**
 * Reads and loads a JSON file into memory
 *
 * @since 0.1.0//  w  ww.  j  ava  2  s  . c om
 * @version 0.1.0
 *
 * @param location The location of the file to parse
 * @throws ParseException If the file is not in standard JSON format
 */
public Json(File location) throws ParseException {
    this.location = location;
    JSONParser parser = new JSONParser();
    JSONObject root = null;
    try {
        root = (JSONObject) parser.parse(new FileReader(this.location));
    } catch (IOException ex) {
        Debugger.error(ex, "Error loading JSON file");
    }
    this.root = root;
}

From source file:cqels_shim.SocketStream.java

/**
 * start listening on the socket and forwarding to cqels
 *///  w w w .  j a  v  a 2 s.c  o m
public void run() {
    ServerSocket ssock = null;
    Socket sock = null;
    try {
        ssock = new ServerSocket(this.port);
        sock = ssock.accept();

        DataInputStream is = new DataInputStream(sock.getInputStream());

        JSONParser parser = new JSONParser();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        String line;

        while ((line = reader.readLine()) != null && !stop) {
            try {
                Object obj = parser.parse(line);
                JSONArray array = (JSONArray) obj;

                //stream the triple
                stream(n((String) array.get(0)), n((String) array.get(1)), n((String) array.get(2)));
            } catch (ParseException pe) {
                System.err.println("Error when parsing input, incorrect JSON.");
            }

            if (sleep > 0) {
                try {
                    Thread.sleep(sleep);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.ibm.bluemixmqtt.MqttHandler.java

/**
 * Received one subscribed message/*  w  w  w.  j  a va  2s. c  om*/
 */
@Override
public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
    String payload = new String(mqttMessage.getPayload());
    JSONParser jpar = new JSONParser();
    org.json.simple.JSONObject job = (org.json.simple.JSONObject) jpar.parse(payload);
    String msg = job.get("d").toString();

    JSONParser jjp = new JSONParser();
    org.json.simple.JSONObject jobn = (org.json.simple.JSONObject) jpar.parse(msg);
    String fmsg = jobn.get("msg").toString();

    System.out.println("IOT Published Message is " + fmsg);
}