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:io.github.casnix.spawnerdropper.Config.java

public static int GetCreeperChance() {
    try {//from   w ww  . jav  a2  s .  com
        // Read entire ./SpawnerDropper/SpawnerDropperConfig.json into a string
        //         System.out.println("[SD DEBUG] 7");
        String configTable = new String(
                Files.readAllBytes(Paths.get("./plugins/SpawnerDropper/SpawnerDropperConfig.json")));

        // get the value of JSON->{spawnerType}
        //      System.out.println("[SD DEBUG] 8");
        JSONParser parser = new JSONParser();

        //   System.out.println("[SD DEBUG] 9");
        Object obj = parser.parse(configTable);

        //System.out.println("[SD DEBUG] 10");
        JSONObject jsonObj = (JSONObject) obj;

        //         System.out.println("[SD DEBUG] 11");
        String chance = (String) jsonObj.get("creeperpercent");
        //      System.out.println("[SD DEBUG] 12");
        return Integer.valueOf(chance);
    } catch (ParseException e) {
        Bukkit.getLogger().warning("[SpawnerDropper] Caught ParseException in GetCreeperChance(void)");
        e.printStackTrace();

        return -1;
    } catch (FileNotFoundException e) {
        Bukkit.getLogger()
                .warning("[SpawnerDropper] Could not find ./plugins/SpawnerDropper/SpawnerDropperConfig.json");
        e.printStackTrace();

        return -1;
    } catch (IOException e) {
        Bukkit.getLogger().warning("[SpawnerDropper] Caught IOException in GetCreeperChance(void)");
        e.printStackTrace();

        return -1;
    } catch (NumberFormatException e) {
        Bukkit.getLogger().warning("[SpawnerDropper] Caught NumberFormatException in GetCreeperChance(void)");
        e.printStackTrace();

        return -1;
    }
}

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;//from www  . j av a 2  s . c om
        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.mycompany.craftdemo.utility.java

public static JSONObject getAPIData(String apiURL) {
    try {/*from w w w  .j  a  v  a  2 s  .  c  o m*/
        URL url = new URL(apiURL);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setRequestProperty("Accept", "application/json");

        if (conn.getResponseCode() != 200) {
            throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
        }

        BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
        StringBuilder sb = new StringBuilder();
        String output;
        while ((output = br.readLine()) != null)
            sb.append(output);

        String res = sb.toString();
        JSONParser parser = new JSONParser();
        JSONObject json = null;
        try {
            json = (JSONObject) parser.parse(res);
        } catch (ParseException ex) {
            ex.printStackTrace();
        }
        //resturn api data in json format
        return json;

    } catch (MalformedURLException e) {

        e.printStackTrace();

    } catch (IOException e) {

        e.printStackTrace();

    }
    return null;
}

From source file:iracing.webapi.TimeTrialResultStandingsParser.java

public static TimeTrialResultStandings parse(String json) {
    TimeTrialResultStandings output = null;
    JSONParser parser = new JSONParser();
    try {//from  w w w.ja  v  a 2  s  .co m
        JSONObject root = (JSONObject) parser.parse(json);
        Object o = root.get("d");
        if (o instanceof JSONObject) {
            JSONObject d = (JSONObject) o;
            output = new TimeTrialResultStandings();
            String s = (String) d.get("3");
            output.setApiUserRow(Integer.parseInt(s));
            output.setTotalRecords(getInt(d, "17"));
            JSONArray arrayRoot = (JSONArray) d.get("r");
            List<TimeTrialResultStanding> standings = new ArrayList<TimeTrialResultStanding>();
            for (int i = 0; i < arrayRoot.size(); i++) {
                JSONObject r = (JSONObject) arrayRoot.get(i);
                TimeTrialResultStanding standing = new TimeTrialResultStanding();
                o = r.get("1");
                if (o instanceof Double) {
                    standing.setPoints((Double) o);
                } else if (o instanceof Long) {
                    standing.setPoints(Double.parseDouble(String.valueOf(o)));
                }
                standing.setLicenseSubLevel(getString(r, "2"));
                standing.setMaxLicenseLevel(getInt(r, "4"));
                standing.setRank(getInt(r, "5"));
                standing.setDivision(getInt(r, "7"));
                standing.setDriverName(getString(r, "8", true));
                standing.setDriverCustomerId(getLong(r, "10"));
                standing.setTotalStarts(getInt(r, "12"));
                standing.setBestTimeFormatted(getString(r, "13", true));
                o = r.get("14");
                if (o instanceof Long) {
                    standing.setBestTime((Long) o);
                } else { // drivers setting no time will have an empty string ("14":"")
                    standing.setBestTime(-1L);
                }
                standing.setClubId(getInt(r, "15"));
                standing.setPosition(getLong(r, "18"));
                standings.add(standing);
            }
            output.setStandings(standings);
        }
    } catch (ParseException ex) {
        Logger.getLogger(TimeTrialResultStandingsParser.class.getName()).log(Level.SEVERE, null, ex);
    }
    return output;
}

From source file:com.ecofactor.qa.automation.util.JsonUtil.java

/**
 * Parses the array.//www . j  a  v a 2  s  . c  o m
 * @param content the content
 * @return the jSON array
 */
public static JSONArray parseArray(final String content) {

    final JSONParser parser = new JSONParser();
    try {
        return (JSONArray) parser.parse(content);
    } catch (ParseException e) {
        LOGGER.error(e.getMessage());
    }
    return null;
}

From source file:com.nubits.nubot.pricefeeds.BitcoinaveragePriceFeed.java

@Override
public LastPrice getLastPrice(CurrencyPair pair) {

    long now = System.currentTimeMillis();
    long diff = now - lastRequest;
    if (diff >= refreshMinTime) {
        String htmlString;//from  w ww .jav a 2  s  .co 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));
            double last = Utils.getDouble(httpAnswerJson.get("last"));
            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.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;/*  ww  w  .jav a 2  s  .c  o  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));
            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.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.CoinmarketcapnorthpolePriceFeed.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 2s  .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));
            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.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.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 w  ww. j  av a2s. co  m*/
            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("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.severe(ex.toString());
            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.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  w w  .  ja  v a 2  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));
            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.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;
    }
}