Example usage for org.json.simple JSONArray iterator

List of usage examples for org.json.simple JSONArray iterator

Introduction

In this page you can find the example usage for org.json.simple JSONArray iterator.

Prototype

public Iterator<E> iterator() 

Source Link

Document

Returns an iterator over the elements in this list in proper sequence.

Usage

From source file:com.nubits.nubot.trading.wrappers.ComkortWrapper.java

@Override
public ApiResponse getActiveOrders(CurrencyPair pair) {
    ApiResponse apiResponse = new ApiResponse();
    String url = API_BASE_URL + "/" + API_ORDER + "/" + API_LIST;
    HashMap<String, String> args = new HashMap<>();
    boolean isGet = false;
    ArrayList<Order> orderList = new ArrayList<>();

    args.put("market_alias", pair.toStringSepSpecial("_"));

    ApiResponse response = getQuery(url, args, true, isGet);
    if (response.isPositive()) {
        JSONObject httpAnswerJson = (JSONObject) response.getResponseObject();
        JSONArray orders = (JSONArray) httpAnswerJson.get("orders");
        for (Iterator<JSONObject> order = orders.iterator(); order.hasNext();) {
            orderList.add(parseOrder(order.next(), pair));
        }//from w w  w  .j  ava 2 s  .  com
        apiResponse.setResponseObject(orderList);
    } else {
        apiResponse = response;
    }

    return apiResponse;
}

From source file:com.nubits.nubot.trading.wrappers.ComkortWrapper.java

private ApiResponse getLastTradesImpl(CurrencyPair pair, long startTime) {
    ApiResponse apiResponse = new ApiResponse();
    //https://api.comkort.com/v1/private/user/trades
    String url = API_BASE_URL + "/" + API_USER + "/" + API_TRADES;
    HashMap<String, String> args = new HashMap<>();
    boolean isGet = false;
    ArrayList<Trade> tradeList = new ArrayList<>();

    args.put("market_alias", pair.toStringSepSpecial("_"));

    ApiResponse response = getQuery(url, args, true, isGet);
    if (response.isPositive()) {
        JSONObject httpAnswerJson = (JSONObject) response.getResponseObject();
        JSONArray trades = (JSONArray) httpAnswerJson.get("trades");
        for (Iterator<JSONObject> trade = trades.iterator(); trade.hasNext();) {
            Trade thisTrade = parseTrade(trade.next());
            if (thisTrade.getDate().getTime() < (startTime * 1000L)) {
                continue;
            }//from   ww  w .  j av a 2  s.  c o  m
            tradeList.add(thisTrade);
        }
        apiResponse.setResponseObject(tradeList);
    } else {
        apiResponse = response;
    }
    return apiResponse;
}

From source file:com.nubits.nubot.trading.wrappers.BitcoinCoIDWrapper.java

private ApiResponse getLastTradesImpl(CurrencyPair pair, long startTime) {
    ApiResponse apiResponse = new ApiResponse();
    String url = API_BASE_URL;
    String method = API_TRADE_HISTORY;
    boolean isGet = false;
    HashMap<String, String> query_args = new HashMap<>();
    ArrayList<Trade> tradeList = new ArrayList<>();

    query_args.put("pair", pair.toStringSep());
    if (startTime > 0) {
        query_args.put("since", Objects.toString(startTime));
    }/*from  w ww.  j  a  v a  2s  .  c  om*/

    ApiResponse response = getQuery(url, method, query_args, true, isGet);
    if (response.isPositive()) {
        JSONObject httpAnswerJson = (JSONObject) response.getResponseObject();
        JSONObject data = (JSONObject) httpAnswerJson.get("return");
        JSONArray trades = (JSONArray) data.get("trades");
        for (Iterator<JSONObject> trade = trades.iterator(); trade.hasNext();) {
            JSONObject thisTrade = trade.next();
            tradeList.add(parseTrade(thisTrade, pair));
        }
        apiResponse.setResponseObject(tradeList);
    } else {
        apiResponse = response;
    }

    return apiResponse;
}

From source file:com.nubits.nubot.trading.wrappers.ComkortWrapper.java

@Override
public ApiResponse getActiveOrders() {
    ApiResponse apiResponse = new ApiResponse();
    String url = API_BASE_URL + "/" + API_ORDER + "/" + API_LIST_ALL;
    HashMap<String, String> args = new HashMap<>();
    boolean isGet = false;
    ArrayList<Order> orderList = new ArrayList<>();

    ApiResponse response = getQuery(url, args, true, isGet);
    if (response.isPositive()) {
        JSONObject httpAnswerJson = (JSONObject) response.getResponseObject();
        JSONObject orders;//from  w w w  .  j  a  v  a2s. co m
        try {
            orders = (JSONObject) httpAnswerJson.get("orders");
        } catch (ClassCastException cce) {
            apiResponse.setResponseObject(orderList);
            return apiResponse;
        }
        Set<String> keys = orders.keySet();
        for (Iterator<String> key = keys.iterator(); key.hasNext();) {
            String thisKey = key.next();
            CurrencyPair thisPair = CurrencyPair.getCurrencyPairFromString(thisKey, "_");
            JSONArray pairOrders = (JSONArray) orders.get(thisKey);
            for (Iterator<JSONObject> order = pairOrders.iterator(); order.hasNext();) {
                orderList.add(parseOrder(order.next(), thisPair));
            }
        }
        apiResponse.setResponseObject(orderList);
    } else {
        apiResponse = response;
    }

    return apiResponse;
}

From source file:es.upm.dit.gsi.seas.SentimenAndEmotiontAnalysisCallingSEAS.java

/**
 * @param eurosentiment/*from www  . j  a v a2 s.  c  om*/
 * @param annotation
 */
public void sentimentAnnotation(String eurosentiment, Annotation annotation) {
    try {
        JSONParser parser = new JSONParser();
        try {
            Object obj = parser.parse(eurosentiment); //Parse SEAS response.
            JSONObject jsonObject = (JSONObject) obj; //Cast into JSON.
            // We take entries
            JSONArray entries = (JSONArray) jsonObject.get("entries");
            Iterator<JSONObject> iterator = entries.iterator();
            while (iterator.hasNext()) { // For each entry
                JSONObject entrie = iterator.next();
                // We take the text
                //String context = (String) entrie.get("nif:isString");
                //docContent = context;
                // We parse the opinions of the text
                JSONArray opinions = (JSONArray) entrie.get("opinions");
                Iterator<JSONObject> iteratorOpinions = opinions.iterator();
                while (iteratorOpinions.hasNext()) {
                    JSONObject opinion = iteratorOpinions.next();
                    // We take the polarity and the value of the text
                    Double textPolarityValue = (Double) opinion.get("marl:polarityValue");
                    String textHasPolarity = (String) opinion.get("marl:hasPolarity");
                    // We create the features for the processed input.
                    FeatureMap sentimentFeatures = Factory.newFeatureMap();
                    sentimentFeatures.put(this.getSentimentPolarityName(), textHasPolarity);
                    sentimentFeatures.put(this.getSentimentValueName(), textPolarityValue);
                    // We put the features in the annotation or in the document.
                    if (annotation != null) {
                        annotation.setFeatures(sentimentFeatures);
                    } else {
                        document.setFeatures(sentimentFeatures);
                    }
                }
                // We take the words in the text with a sentiment value and polarity
                //              JSONArray strings = (JSONArray) entrie.get("strings");
                //              Iterator<JSONObject> iteratorStrings = strings.iterator();
                //              while (iteratorStrings.hasNext()) { // For each word we take its values.
                //                 JSONObject string = iteratorStrings.next();
                //
                //                 String word= (String) string.get("nif:anchorOf");
                //                 Long beginIndex = (Long) string.get("nif:beginIndex");
                //                 Long endIndex = (Long) string.get("nif:endIndex");
                //                 JSONObject stringOpinion = (JSONObject) string.get("opinions");
                //                 Double stringPolarityValue= (Double) stringOpinion.get("marl:polarityValue");
                //                 String stringHasPolarity = (String) stringOpinion.get("marl:hasPolarity");
                //              }
            }
        } catch (Exception e) {
            System.out.println("The JSON is not parsed.");
            System.out.println("The response of the service has been: " + eurosentiment);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.nubits.nubot.trading.wrappers.BittrexWrapper.java

private ApiResponse getActiveOrdersImpl(CurrencyPair pair) {
    ApiResponse apiResponse = new ApiResponse();
    String url = API_BASE_URL;
    String method = API_ACTIVE_ORDERS;
    HashMap<String, String> args = new HashMap<>();
    boolean isGet = true;
    boolean needAuth = true;
    ArrayList<Order> orderList = new ArrayList<>();

    if (pair != null) {
        args.put("market", pair.toStringSepInverse("-"));
    }//from  w  w w .  j ava2s . co m

    ApiResponse response = getQuery(url, method, args, needAuth, isGet);
    if (response.isPositive()) {
        JSONObject httpAnswerJson = (JSONObject) response.getResponseObject();
        JSONArray result = (JSONArray) httpAnswerJson.get("result");
        for (Iterator<JSONObject> order = result.iterator(); order.hasNext();) {
            JSONObject thisOrder = order.next();
            orderList.add(parseOrder(thisOrder));
        }
        apiResponse.setResponseObject(orderList);
    } else {
        apiResponse = response;
    }

    return apiResponse;
}

From source file:com.nubits.nubot.trading.wrappers.BittrexWrapper.java

private ApiResponse getLastTradesImpl(CurrencyPair pair, long startTime) {
    ApiResponse apiResponse = new ApiResponse();
    String url = API_BASE_URL;
    String method = API_LAST_ORDERS;
    HashMap<String, String> args = new HashMap<>();
    boolean isGet = true;
    boolean needAuth = true;
    ArrayList<Trade> tradeList = new ArrayList<>();

    ApiResponse response = getQuery(url, method, args, needAuth, isGet);
    if (response.isPositive()) {
        JSONObject httpAnswerJson = (JSONObject) response.getResponseObject();
        JSONArray result = (JSONArray) httpAnswerJson.get("result");
        for (Iterator<JSONObject> trade = result.iterator(); trade.hasNext();) {
            Trade thisTrade = parseTrade(trade.next());
            if (thisTrade.getDate() == null) {
                continue;
            }//from   w  w w .j a v  a  2 s  .c  o m
            if (startTime > 0 && thisTrade.getDate().getTime() < startTime) {
                continue;
            }
            tradeList.add(thisTrade);
        }
        apiResponse.setResponseObject(tradeList);
    } else {
        apiResponse = response;
    }

    return apiResponse;
}

From source file:com.nubits.nubot.trading.wrappers.BitcoinCoIDWrapper.java

private ApiResponse getActiveOrdersImpl(CurrencyPair pair, boolean convertBuyAmounts) {
    ApiResponse apiResponse = new ApiResponse();
    String url = API_BASE_URL;
    String method = API_OPEN_ORDERS;
    ArrayList<Order> orderList = new ArrayList<>();
    HashMap<String, String> query_args = new HashMap<>();
    boolean isGet = false;

    //only handles openOrders with given pair

    if (pair != null) {
        query_args.put("pair", pair.toStringSep());
    } else {/*from   www.  j av  a2s  . c om*/
        pair = Global.options.getPair();
        query_args.put("pair", pair.toStringSep());
    }

    ApiResponse response = getQuery(url, method, query_args, true, isGet);
    if (response.isPositive()) {
        JSONObject httpAnswerJson = (JSONObject) response.getResponseObject();
        JSONObject data = (JSONObject) httpAnswerJson.get("return");
        JSONArray orders = (JSONArray) data.get("orders");
        if (orders != null) {
            for (Iterator<JSONObject> order = orders.iterator(); order.hasNext();) {
                JSONObject thisOrder = order.next();
                orderList.add(parseOrder(thisOrder, pair, convertBuyAmounts));
            }
        }
        apiResponse.setResponseObject(orderList);
    } else {
        apiResponse = response;
    }
    return apiResponse;
}

From source file:com.ge.research.semtk.load.utility.ImportSpecHandler.java

/**
 * Build a value from a mapping array and a data record
 * @param mappingArrayJson// w  w w .j  a  v  a  2  s.com
 * @param record
 * @return result or Null if any column is empty
 * @throws Exception
 */
private String buildMappingString(JSONArray mappingArrayJson, ArrayList<String> record) throws Exception {
    String ret = "";

    // loop through the mapping array
    Iterator<JSONObject> it = mappingArrayJson.iterator();
    for (int i = 0; i < mappingArrayJson.size(); i++) {
        JSONObject mapItem = (JSONObject) mappingArrayJson.get(i);

        if (mapItem.containsKey("textId")) {
            String text = null;
            // get text id
            String id = mapItem.get("textId").toString();

            // look up text
            try {
                text = this.textsAvailable.get(id);
            } catch (Exception e) {
                throw new Exception("Failed to look up textId: " + id);
            }

            // if all is well, append the value
            if (!StringUtils.isEmpty(text)) {
                ret += text;
            }

        } else if (mapItem.containsKey("colId")) {
            String colText = null;
            Integer pos = null;
            String id = mapItem.get("colId").toString();
            try {
                String textColLabel = this.colsAvailable.get(id);
                pos = this.headerPositioningInfo.get(textColLabel);
                colText = record.get(pos); // set the text equal to the correct column.                
            } catch (Exception e) {
                colText = "";
                if (pos == null) {
                    throw new Exception("Cannot find column in header list.");
                }
            }

            if (!StringUtils.isBlank(colText)) {
                ret += this.applyTransforms(colText, mapItem);
            } else {
                // found an empty column
                return null;
            }
        } else {
            throw new Exception("importSpec mapping item has no known type: " + mapItem.toString());
        }
    }
    return ret;
}

From source file:net.sourceforge.fenixedu.presentationTier.Action.gep.a3es.A3ESDegreeProcess.java

public void initialize() {
    base64Hash = new String(Base64.getEncoder().encode((user + ":" + password).getBytes()));
    JSONArray processes = invoke(webResource().path(API_PROCESS));
    JSONObject json = (JSONObject) processes.iterator().next();
    id = (String) json.get("id");
    String name = (String) json.get("name");
    if (acefIndex.containsKey(name)) {
        degree = Degree.readBySigla(acefIndex.get(name));
        JSONArray forms = invoke(webResource().path(API_FORM).queryParam("processId", id));
        for (Object object : forms) {
            JSONObject form = (JSONObject) object;
            if ("Guio para a auto-avaliao".equals(form.get("name"))) {
                formId = (String) form.get("id");
            }/*w  ww  . j a  v a2s . co m*/
        }
        if (formId == null) {
            throw new DomainException("Process " + name + " has no auto-evaluation form.");
        }
    } else {
        throw new DomainException("Not recognized ACEF code: " + name);
    }
}