List of usage examples for org.json JSONArray length
public int length()
From source file:au.com.borner.salesforce.client.rest.domain.AbstractJSONObject.java
public List<String> toStringList(JSONArray array) { try {//from w w w . j av a 2 s . c om List<String> result = new ArrayList<String>(array.length()); for (int i = 0; i < array.length(); i++) { result.add(array.getString(i)); } return result; } catch (JSONException e) { return Collections.emptyList(); } }
From source file:au.com.borner.salesforce.client.rest.domain.AbstractJSONObject.java
@SuppressWarnings("unchecked") protected <T extends AbstractJSONObject> List<T> getChildEntities(String name, Class<T> resultClass) { try {/*w ww . ja v a2s. com*/ Constructor<T> constructor = resultClass.getConstructor(String.class); JSONArray children = getJSONArray(name); List<T> result = new ArrayList<T>(children.length()); for (int i = 0; i < children.length(); i++) { JSONObject jsonObject = children.getJSONObject(i); T instance = constructor.newInstance(jsonObject.toString()); // is there any way to avoid double serialisation? result.add(instance); } return result; } catch (Exception e) { throw new RuntimeException("Error parsing child elements", e); } }
From source file:account.management.controller.inventory.PurchaseReportController.java
@FXML private void onShowReportButtonClick(ActionEvent event) { this.show.setDisable(true); try {// ww w . j a v a 2 s . com String id = String.valueOf(this.item.getSelectionModel().getSelectedItem().getId()); String start_date = "1980-01-01", end_date = "2050-12-31"; start_date = new SimpleDateFormat("yyyy-MM-dd") .format(new SimpleDateFormat("yyyy-MM-dd").parse(this.start.getValue().toString())); end_date = new SimpleDateFormat("yyyy-MM-dd") .format(new SimpleDateFormat("yyyy-MM-dd").parse(this.end.getValue().toString())); HttpResponse<JsonNode> res = Unirest.get(MetaData.baseUrl + "report/ledger/purchase") .queryString("id", id).queryString("start", start_date).queryString("end", end_date).asJson(); JSONArray array = res.getBody().getArray(); System.out.println(array); Vector v = new Vector(); HashMap params = new HashMap(); params.put("date", "From " + new SimpleDateFormat("dd-MM-yyyy") .format(new SimpleDateFormat("yyyy-MM-dd").parse(start_date)) + " To " + new SimpleDateFormat("dd-MM-yyyy") .format(new SimpleDateFormat("yyyy-MM-dd").parse(end_date))); params.put("item_name", "Purchase report of " + this.item.getSelectionModel().getSelectedItem().getName()); for (int i = 0; i < array.length(); i++) { JSONObject obj = array.getJSONObject(i); String date = obj.getString("date"); String quantity = obj.get("quantity").toString(); String rate = obj.get("rate").toString(); v.add(new SellPurchaseLedger(date, quantity, rate)); } Report report = new Report(); report.getReport("src\\report\\SellPurchaseLedger.jrxml", new JRBeanCollectionDataSource(v), params, "Purchase Report"); this.show.setDisable(false); } catch (Exception e) { System.out.println("Exception in show report button click"); Alert alert = new Alert(Alert.AlertType.ERROR); alert.setHeaderText(null); alert.setContentText("Sorry!! there is an error. Please try again."); alert.setGraphic(new ImageView(new Image("resources/error.jpg"))); alert.showAndWait(); } }
From source file:com.clevertrail.mobile.findtrail.Activity_FindTrail_ByName.java
private int submitSearch() { JSONArray jsonArray = null; HttpURLConnection urlConnection = null; try {//from w w w .java 2 s.co m //api call to find trails by name String requestURL = "http://clevertrail.com/ajax/handleGetArticles.php"; String sSearch = mActivity.sSearchText; sSearch = sSearch.replace(" ", "%20"); sSearch = sSearch.replace("'", "\'"); requestURL = requestURL.concat("?name=").concat(sSearch); URL url = new URL(requestURL); urlConnection = (HttpURLConnection) url.openConnection(); InputStream in = new BufferedInputStream(urlConnection.getInputStream()); BufferedReader r = new BufferedReader(new InputStreamReader(in)); String line; //did we get a match from clevertrail.com? if ((line = r.readLine()) != null) { jsonArray = new JSONArray(line); } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); return R.string.error_contactingclevertrail; } catch (IOException e) { // TODO Auto-generated catch block // could not connect to clevertrail.com e.printStackTrace(); return R.string.error_contactingclevertrail; } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); return R.string.error_corrupttrailinfo; } finally { if (urlConnection != null) urlConnection.disconnect(); } //clear the static object that holds the trails Object_TrailList.clearTrails(); if (jsonArray != null && jsonArray.length() > 0) { int len = jsonArray.length(); try { //add each trail to the array of trails for (int i = 0; i < len && i < 20; ++i) { JSONObject trail = jsonArray.getJSONObject(i); Object_TrailList.addTrailWithJSON(trail); } } catch (JSONException e) { return R.string.error_corrupttrailinfo; } } //prepare the extras before starting the trail list activity int icon = R.drawable.ic_viewtrailtab_details_unselected; Intent i = new Intent(mActivity, Activity_ListTrails.class); i.putExtra("icon", icon); i.putExtra("title", mActivity.getString(R.string.title_foundtrails)); mActivity.startActivity(i); return 0; }
From source file:org.everit.osgi.webconsole.configuration.ConfigServlet.java
private Map<String, List<String>> extractRawAttributesFromJSON(final JSONObject json) { Map<String, List<String>> rawAttributes = new HashMap<>(); for (Object rawKey : json.keySet()) { String key = (String) rawKey; JSONArray valueArr = (JSONArray) json.get(key); int stringCount = valueArr.length(); List<String> values = new ArrayList<String>(stringCount); for (int i = 0; i < stringCount; ++i) { Object value = valueArr.get(i); if (value.equals(JSONObject.NULL)) { values.add(null);// w w w .j av a 2s . c o m } else { values.add(value.toString()); } } rawAttributes.put(key, values); } return rawAttributes; }
From source file:org.achtern.AchternEngine.core.resource.loader.json.JsonLoader.java
/** * Converts a {@link org.json.JSONArray} into an array of * {@link java.lang.Object}s/* w w w. j a v a 2 s .c om*/ * @param array JSONArray * @return Object array */ public static Object[] toObjectArray(JSONArray array) { Object[] objects = new Object[array.length()]; for (int i = 0; i < objects.length; i++) { objects[i] = array.get(i); } return objects; }
From source file:com.fabernovel.alertevoirie.utils.JSONAdapter.java
public JSONAdapter(Context context, JSONArray data, int cellLayout, String[] from, int[] to, String jsonObjectName, String categoryField, int categoryLayout) { this(context, data, cellLayout, from, to, jsonObjectName); this.categoryField = categoryField; this.categoryLayout = categoryLayout; String currentCat = null;//ww w.j a va 2s . co m categories = new SparseArray<String>(2); if (data != null) for (int i = 0, j = 0; i < data.length(); i++, j++) { String cat = getCategoryOfItem(i); Log.d(Constants.PROJECT_TAG, "catgorie : " + cat); if (!cat.equals(currentCat)) { Log.d(Constants.PROJECT_TAG, "category " + j + " : " + cat); categories.put(j++, cat); currentCat = cat; } } }
From source file:com.cssweb.android.view.KlineView.java
/** * ??/*w w w . j a v a 2 s. c om*/ * @throws JSONException */ private void makeTodayData() throws JSONException { int l = quoteData.getJSONArray("K").length(); if (quoteData.isNull("joTMP")) {//temp?? newStockhandler(); return; } if (!quoteData.getBoolean("tradeFlag")) { //?????? //???0?? return; } JSONObject tempvalue = quoteData.getJSONObject("joTMP"); double zrsp = quoteData.getDouble("zrsp"); period = quoteData.getString("period"); if (period.equals("week") || period.equals("month") || period.equals("year")) { if (tempvalue.getString(period) != null) { if (tempvalue.isNull("ma") || quoteData.isNull("MA") || quoteData.isNull("K")) { makeTmpData(); return; } int tp = quoteData.getInt("tp"); if (tp == 1) { String date = tempvalue.getJSONObject(period).getString("date"); if (DateTool.isSameWeekMonthYear(date, period)) { quoteData.getJSONArray("K").getJSONArray(l - 1).put(1, tempvalue.getJSONObject(period).getDouble("open")); quoteData.getJSONArray("K").getJSONArray(l - 1).put(2, tempvalue.getJSONObject(period).getDouble("high")); quoteData.getJSONArray("K").getJSONArray(l - 1).put(3, tempvalue.getJSONObject(period).getDouble("low")); quoteData.getJSONArray("K").getJSONArray(l - 1).put(4, zrsp); quoteData.getJSONArray("K").getJSONArray(l - 1).put(5, tempvalue.getJSONObject(period).getDouble("cjsl")); quoteData.getJSONArray("K").getJSONArray(l - 1).put(6, tempvalue.getJSONObject(period).getDouble("cjje")); //quoteData.getJSONArray("K").getJSONArray(l-1).put(0, ) ; zrsp = tempvalue.getJSONObject(period).getDouble("close"); Log.i("#####period->zrsp####", quoteData.getJSONArray("K").getJSONArray(l - 1).getDouble(4) + ">>>>>>>>" + zrsp); } } else { int spayday = quoteData.getInt("spday"); double cjsl = quoteData.getJSONArray("K").getJSONArray(l - 1).getDouble(5); double cjje = quoteData.getJSONArray("K").getJSONArray(l - 1).getDouble(6); if (spayday == 0) {//0??temp????? quoteData.getJSONArray("K").getJSONArray(l - 1).put(2, Math.max(quoteData.getJSONArray("K").getJSONArray(l - 1).getDouble(2), tempvalue.getJSONObject(period).getDouble("high"))); quoteData.getJSONArray("K").getJSONArray(l - 1).put(3, Math.min(quoteData.getJSONArray("K").getJSONArray(l - 1).getDouble(3), tempvalue.getJSONObject(period).getDouble("low"))); quoteData.getJSONArray("K").getJSONArray(l - 1).put(5, cjsl + tempvalue.getJSONObject(period).getDouble("cjsl")); quoteData.getJSONArray("K").getJSONArray(l - 1).put(6, cjje + tempvalue.getJSONObject(period).getDouble("cjje")); } else { quoteData.getJSONArray("K").getJSONArray(l - 1).put(2, Math.max(quoteData.getJSONArray("K").getJSONArray(l - 1).getDouble(2), tempvalue.getJSONObject(period).getDouble("high"))); quoteData.getJSONArray("K").getJSONArray(l - 1).put(3, Math.min(quoteData.getJSONArray("K").getJSONArray(l - 1).getDouble(3), tempvalue.getJSONObject(period).getDouble("low"))); quoteData.getJSONArray("K").getJSONArray(l - 1).put(5, tempvalue.getJSONObject(period).getDouble("cjsl")); quoteData.getJSONArray("K").getJSONArray(l - 1).put(6, tempvalue.getJSONObject(period).getDouble("cjje")); } double jrkp = tempvalue.getJSONObject(period).getDouble("open"); if (jrkp != 0) { quoteData.getJSONArray("K").getJSONArray(l - 1).put(1, jrkp); } zrsp = tempvalue.getJSONObject(period).getDouble("close"); } } } String qt = quoteData.getJSONArray("K").getJSONArray(l - 1).getString(0); double high = quoteData.getJSONArray("K").getJSONArray(l - 1).getDouble(2); double low = quoteData.getJSONArray("K").getJSONArray(l - 1).getDouble(3); double zjcj = quoteData.getJSONArray("K").getJSONArray(l - 1).getDouble(4); double cjsl = quoteData.getJSONArray("K").getJSONArray(l - 1).getDouble(5); double summa4 = tempvalue.getJSONObject("ma").getDouble("sumMa4"); double summa9 = tempvalue.getJSONObject("ma").getDouble("sumMa9"); double summa19 = tempvalue.getJSONObject("ma").getDouble("sumMa19"); double summa59 = tempvalue.getJSONObject("ma").getDouble("sumMa59"); double sumvolma4 = tempvalue.getJSONObject("ma").getDouble("sumMavol4"); double sumvolma9 = tempvalue.getJSONObject("ma").getDouble("sumMavol9"); if (mainIndicatorType.toUpperCase().equals("MA") || mainIndicatorType.toUpperCase().equals("BOLL")) { quoteData.getJSONArray("MA").put(new JSONArray()); quoteData.getJSONArray("MA").getJSONArray(l - 1).put(0, qt); double ma5 = 0; if (l > 4) ma5 = (summa4 + zjcj) / 5; quoteData.getJSONArray("MA").getJSONArray(l - 1).put(1, ma5); double ma10 = 0; if (l > 9) ma10 = (summa9 + zjcj) / 10; quoteData.getJSONArray("MA").getJSONArray(l - 1).put(2, ma10); double ma20 = 0; if (l > 19) ma20 = (summa19 + zjcj) / 20; quoteData.getJSONArray("MA").getJSONArray(l - 1).put(3, ma20); double ma60 = 0; if (l > 59) ma60 = (summa59 + zjcj) / 60; quoteData.getJSONArray("MA").getJSONArray(l - 1).put(4, ma60); } double mavol5 = (sumvolma4 + cjsl) / 5; quoteData.getJSONArray("MA").getJSONArray(l - 1).put(5, mavol5); double mavol10 = (sumvolma9 + cjsl) / 10; quoteData.getJSONArray("MA").getJSONArray(l - 1).put(6, mavol10); if (mainIndicatorType.equals("BOLL")) { quoteData.getJSONArray("BOLL").put(new JSONArray()); double mid = 0; double upper = 0; double lower = 0; if (l > 25) { double sumClose = tempvalue.getJSONObject("boll").getDouble("sumClose"); double sumPowClose = tempvalue.getJSONObject("boll").getDouble("sumPowClose"); double maPow = 0; double temp; if (l > 25) { mid = (sumClose + zjcj) / 26; maPow = (sumPowClose + zjcj * zjcj) / 26; temp = (maPow - mid * mid); if (temp < 0) temp = 0; upper = mid + 2 * Math.sqrt((temp * 26) / (26 - 1)); lower = mid - 2 * Math.sqrt((temp * 26) / (26 - 1)); } } quoteData.getJSONArray("BOLL").getJSONArray(l - 1).put(0, qt); quoteData.getJSONArray("BOLL").getJSONArray(l - 1).put(1, mid); quoteData.getJSONArray("BOLL").getJSONArray(l - 1).put(2, upper); quoteData.getJSONArray("BOLL").getJSONArray(l - 1).put(3, lower); } if (indicatorType.equals("MACD")) { quoteData.getJSONArray("MACD").put(new JSONArray()); double prevemashort = 0; double prevemalong = 0; double prevdea = 0; double dif = 0; double dea = 0; double macd = 0; double emashort = 0; double emalong = 0; prevemashort = tempvalue.getJSONObject("macd").getDouble("emaShort"); prevemalong = tempvalue.getJSONObject("macd").getDouble("emaLong"); prevdea = tempvalue.getJSONObject("macd").getDouble("dea"); if (l > 1) { emashort = (2 * zjcj + (12 - 1) * prevemashort) / (12 + 1); emalong = (2 * zjcj + (26 - 1) * prevemalong) / (26 + 1); dif = emashort - emalong; dea = (2 * dif + (9 - 1) * prevdea) / (9 + 1); macd = (dif - dea) * 2; } quoteData.getJSONArray("MACD").getJSONArray(l - 1).put(0, qt); quoteData.getJSONArray("MACD").getJSONArray(l - 1).put(1, dif); quoteData.getJSONArray("MACD").getJSONArray(l - 1).put(2, dea); quoteData.getJSONArray("MACD").getJSONArray(l - 1).put(3, macd); } if (indicatorType.equals("BIAS")) { quoteData.getJSONArray("BIAS").put(new JSONArray()); double sum5 = tempvalue.getJSONObject("bias").getDouble("sum5"); double sum11 = tempvalue.getJSONObject("bias").getDouble("sum11"); double sum23 = tempvalue.getJSONObject("bias").getDouble("sum23"); double bias1 = l >= 6 ? (zjcj - (sum5 + zjcj) / 6) / ((sum5 + zjcj) / 6) * 100 : 0; double bias2 = l >= 12 ? (zjcj - (sum11 + zjcj) / 12) / ((sum11 + zjcj) / 12) * 100 : 0; double bias3 = l >= 24 ? (zjcj - (sum23 + zjcj) / 24) / ((sum23 + zjcj) / 24) * 100 : 0; quoteData.getJSONArray("BIAS").getJSONArray(l - 1).put(0, qt); quoteData.getJSONArray("BIAS").getJSONArray(l - 1).put(1, bias1); quoteData.getJSONArray("BIAS").getJSONArray(l - 1).put(2, bias2); quoteData.getJSONArray("BIAS").getJSONArray(l - 1).put(3, bias3); } if (indicatorType.equals("RSI")) { quoteData.getJSONArray("RSI").put(new JSONArray()); double rsi1 = 0; double rsi2 = 0; double rsi3 = 0; if (l > 1 && tempvalue.has("rsi")) { double smaMax1 = tempvalue.getJSONObject("rsi").getDouble("smaMax1"); double smaMax2 = tempvalue.getJSONObject("rsi").getDouble("smaMax2"); double smaMax3 = tempvalue.getJSONObject("rsi").getDouble("smaMax3"); double smaAbs1 = tempvalue.getJSONObject("rsi").getDouble("smaAbs1"); double smaAbs2 = tempvalue.getJSONObject("rsi").getDouble("smaAbs2"); double smaAbs3 = tempvalue.getJSONObject("rsi").getDouble("smaAbs3"); double rsiMax1 = (Math.max(zjcj - zrsp, 0.0) * 1 + smaMax1 * (6 - 1)) / 6; double rsiAbs1 = (Math.abs(zjcj - zrsp) * 1 + smaAbs1 * (6 - 1)) / 6; if (rsiAbs1 == 0) rsi1 = 0; else rsi1 = rsiMax1 / rsiAbs1 * 100; double rsiMax2 = (Math.max(zjcj - zrsp, 0.0) * 1 + smaMax2 * (12 - 1)) / 12; double rsiAbs2 = (Math.abs(zjcj - zrsp) * 1 + smaAbs2 * (12 - 1)) / 12; if (rsiAbs2 == 0) rsi2 = 0; else rsi2 = rsiMax2 / rsiAbs2 * 100; double rsiMax3 = (Math.max(zjcj - zrsp, 0.0) * 1 + smaMax3 * (24 - 1)) / 24; double rsiAbs3 = (Math.abs(zjcj - zrsp) * 1 + smaAbs3 * (24 - 1)) / 24; if (rsiAbs3 == 0) rsi3 = 0; else rsi3 = rsiMax3 / rsiAbs3 * 100; } quoteData.getJSONArray("RSI").getJSONArray(l - 1).put(0, qt); quoteData.getJSONArray("RSI").getJSONArray(l - 1).put(1, rsi1); quoteData.getJSONArray("RSI").getJSONArray(l - 1).put(2, rsi2); quoteData.getJSONArray("RSI").getJSONArray(l - 1).put(3, rsi3); } if (indicatorType.equals("KDJ")) { quoteData.getJSONArray("KDJ").put(new JSONArray()); double newk = 0; double newd = 0; double newj = 0; if (l > 1 && tempvalue.has("kdj")) { double K = quoteData.getJSONArray("KDJ").getJSONArray(l - 2).getDouble(1); double D = quoteData.getJSONArray("KDJ").getJSONArray(l - 2).getDouble(2); double HHV = tempvalue.getJSONObject("kdj").getDouble("hhv"); double LLV = tempvalue.getJSONObject("kdj").getDouble("llv"); double nowllv = 0.0; if (LLV < low) // l? nowllv = LLV; else nowllv = low; double nowhhv = 0.0; if (HHV > high) nowhhv = HHV; else nowhhv = high; double rsv; if (Math.abs(nowhhv - nowllv) < 0.0001) { rsv = 0; } else { rsv = (zjcj - nowllv) / (nowhhv - nowllv) * 100; } newk = (rsv * 1 + K * (3 - 1)) / 3; newd = (newk * 1 + D * (3 - 1)) / 3; newj = 3 * newk - 2 * newd; } quoteData.getJSONArray("KDJ").getJSONArray(l - 1).put(0, qt); quoteData.getJSONArray("KDJ").getJSONArray(l - 1).put(1, newk); quoteData.getJSONArray("KDJ").getJSONArray(l - 1).put(2, newd); quoteData.getJSONArray("KDJ").getJSONArray(l - 1).put(3, newj); } if (indicatorType.equals("CCI")) { double CCI = 0; quoteData.getJSONArray("CCI").put(new JSONArray()); if (l > 13 && tempvalue.has("cci")) { JSONArray typlist = tempvalue.getJSONObject("cci").getJSONArray("typ"); double sumTyp = 0; double TYP = (zjcj + high + low) / 3; double rit = 0; for (int i = 0; i < typlist.length(); i++) { rit = typlist.getDouble(i); if (i == 13) break; sumTyp += rit; } sumTyp += TYP; double ma = sumTyp / 14; sumTyp = 0; for (int i = 0; i < typlist.length(); i++) { rit = typlist.getDouble(i); if (i == 13) break; sumTyp += Math.abs(rit - ma); } sumTyp += Math.abs(TYP - ma); double avedev = sumTyp / 14; if (avedev == 0) CCI = 0; else CCI = (TYP - ma) / (0.015 * avedev); } quoteData.getJSONArray("CCI").getJSONArray(l - 1).put(0, qt); quoteData.getJSONArray("CCI").getJSONArray(l - 1).put(1, CCI); } if (indicatorType.equals("OBV")) { quoteData.getJSONArray("OBV").put(new JSONArray()); double obv = 0, maobv = 0; if (tempvalue.has("obv")) { if (zjcj > zrsp) { obv = tempvalue.getJSONObject("obv").getDouble("obv") + cjsl; } if (zjcj == zrsp) { obv = 0; } if (zjcj < zrsp) { obv = tempvalue.getJSONObject("obv").getDouble("obv") - cjsl; } if (l >= 29) { maobv = (tempvalue.getJSONObject("obv").getDouble("sumObv29") + obv) / 30; } else { maobv = 0; } } quoteData.getJSONArray("OBV").getJSONArray(l - 1).put(0, qt); quoteData.getJSONArray("OBV").getJSONArray(l - 1).put(1, obv); quoteData.getJSONArray("OBV").getJSONArray(l - 1).put(2, maobv); } if (indicatorType.equals("PSY")) { quoteData.getJSONArray("PSY").put(new JSONArray()); double countPsy = 0, psy = 0, psyma = 0; if (zjcj > zrsp) { countPsy = 1; } else { countPsy = 0; } if (l >= 11 && tempvalue.has("psy")) { countPsy += tempvalue.getJSONObject("psy").getDouble("psyCount11"); psy = countPsy / 12 * 100; psyma = (tempvalue.getJSONObject("psy").getDouble("sumPsy") + psy) / 6; } else { psy = 0; } quoteData.getJSONArray("PSY").getJSONArray(l - 1).put(0, qt); quoteData.getJSONArray("PSY").getJSONArray(l - 1).put(1, psy); quoteData.getJSONArray("PSY").getJSONArray(l - 1).put(2, psyma); } if (indicatorType.equals("ROC")) { quoteData.getJSONArray("ROC").put(new JSONArray()); double roc = 0, rocma = 0; if (l > 12 && tempvalue.has("roc")) { double refClose12 = tempvalue.getJSONObject("roc").getDouble("refClose12"); if (refClose12 == 0) { roc = 0; } else { roc = 100 * (zjcj - refClose12) / refClose12; } rocma = (tempvalue.getJSONObject("roc").getDouble("sumRoc") + roc) / 6; } else { roc = 0; } quoteData.getJSONArray("ROC").getJSONArray(l - 1).put(0, qt); quoteData.getJSONArray("ROC").getJSONArray(l - 1).put(1, roc); quoteData.getJSONArray("ROC").getJSONArray(l - 1).put(2, rocma); } if (indicatorType.equals("WR")) { quoteData.getJSONArray("WR").put(new JSONArray()); double wr = 0, wr2 = 0; if (l > 9 && tempvalue.has("wr")) { double llv = tempvalue.getJSONObject("wr").getDouble("llv"); double hhv = tempvalue.getJSONObject("wr").getDouble("hhv"); hhv = Math.max(hhv, high); llv = Math.min(llv, low); if (hhv == llv) { wr = 0; } else { wr = 100 * (hhv - zjcj) / (hhv - llv); } } else { wr = 0; } if (l > 5 && tempvalue.has("wr")) { double llv2 = tempvalue.getJSONObject("wr").getDouble("llv2"); double hhv2 = tempvalue.getJSONObject("wr").getDouble("hhv2"); hhv2 = Math.max(hhv2, high); llv2 = Math.min(llv2, low); if (hhv2 == llv2) { wr2 = 0; } else { wr2 = 100 * (hhv2 - zjcj) / (hhv2 - llv2); } } else { wr2 = 0; } quoteData.getJSONArray("WR").getJSONArray(l - 1).put(0, qt); quoteData.getJSONArray("WR").getJSONArray(l - 1).put(1, wr); quoteData.getJSONArray("WR").getJSONArray(l - 1).put(2, wr2); } if (indicatorType.equals("VR")) { quoteData.getJSONArray("VR").put(new JSONArray()); double vr = 0, vrma = 0; if (l >= 24 && tempvalue.has("vr")) { double sum1 = tempvalue.getJSONObject("vr").getDouble("sum1"); double sum2 = tempvalue.getJSONObject("vr").getDouble("sum2"); if (zjcj > zrsp) { sum1 += cjsl; } else { sum2 += cjsl; } if (sum2 == 0) { vr = 0; } else { vr = 100 * sum1 / sum2; } } else { vr = 0; } if (l >= 6) vrma = (tempvalue.getJSONObject("vr").getDouble("sumVr") + vr) / 6; quoteData.getJSONArray("VR").getJSONArray(l - 1).put(0, qt); quoteData.getJSONArray("VR").getJSONArray(l - 1).put(1, vr); quoteData.getJSONArray("VR").getJSONArray(l - 1).put(2, vrma); } }
From source file:nl.spellenclubeindhoven.dominionshuffle.Application.java
public void loadResult() { try {/*from www . ja v a2 s.c o m*/ JSONObject jsonResult = new JSONObject(DataReader.readStringFromFile(this, "result.json")); JSONArray jsonCards = jsonResult.getJSONArray("cards"); LinkedList<Card> cards = new LinkedList<Card>(); for (int i = 0; i < jsonCards.length(); i++) { Card card = dataReader.getData().getCard(jsonCards.getString(i)); if (card == null) return; cards.add(card); } Card baneCard = null; Card obeliskCard = null; if (jsonResult.has("baneCard")) { baneCard = dataReader.getData().getCard(jsonResult.getString("baneCard")); } if (jsonResult.has("obeliskCard")) { obeliskCard = dataReader.getData().getCard(jsonResult.getString("obeliskCard")); } result = new Result(); result.setCards(cards); result.setBaneCard(baneCard); result.setObeliskCard(obeliskCard); } catch (JSONException ignore) { ignore.printStackTrace(); } }
From source file:com.atinternet.tracker.Builder.java
/** * Prepare the hit queryString/*from ww w. j ava2 s . c om*/ * * @return LinkedHashMap */ private LinkedHashMap<String, Object[]> prepareQuery() { LinkedHashMap<String, Object[]> formattedParameters = new LinkedHashMap<String, Object[]>(); ArrayList<Param> completeBuffer = new ArrayList<Param>() { { addAll(persistentParams); addAll(volatileParams); } }; ArrayList<Param> params = organizeParameters(completeBuffer); for (Param p : params) { String value = p.getValue().execute(); String key = p.getKey(); HashMap<String, String> plugins = PluginParam.get(tracker); if (plugins.containsKey(key)) { String pluginClass = plugins.get(key); Plugin plugin = null; try { plugin = (Plugin) Class.forName(pluginClass).newInstance(); plugin.execute(tracker); value = plugin.getResponse(); p.setType(Param.Type.JSON); key = Hit.HitParam.JSON.stringValue(); } catch (Exception e) { e.printStackTrace(); value = null; } } else if (key.equals(Hit.HitParam.UserId.stringValue())) { if (TechnicalContext.doNotTrackEnabled(Tracker.getAppContext())) { value = OPT_OUT; } else if (((Boolean) configuration.get(TrackerConfigurationKeys.HASH_USER_ID))) { value = Tool.SHA_256(value); } } if (p.getType() == Param.Type.Closure && Tool.parseJSON(value) != null) { p.setType(Param.Type.JSON); } if (value != null) { // Referrer processing if (key.equals(Hit.HitParam.Referrer.stringValue())) { value = value.replace("&", "$").replaceAll("[<>]", ""); } if (p.getOptions() != null && p.getOptions().isEncode()) { value = Tool.percentEncode(value); p.getOptions().setSeparator(Tool.percentEncode(p.getOptions().getSeparator())); } int duplicateParamIndex = -1; String duplicateParamKey = null; Set<String> keys = formattedParameters.keySet(); String[] keySet = keys.toArray(new String[keys.size()]); int length = keySet.length; for (int i = 0; i < length; i++) { if (keySet[i].equals(key)) { duplicateParamIndex = i; duplicateParamKey = key; break; } } if (duplicateParamIndex != -1) { List<Object[]> values = new ArrayList<Object[]>(formattedParameters.values()); Param duplicateParam = (Param) values.get(duplicateParamIndex)[0]; String str = ((String) formattedParameters.get(duplicateParamKey)[1]).split("=")[0] + "="; String val = ((String) formattedParameters.get(duplicateParamKey)[1]).split("=")[1]; if (p.getType() == Param.Type.JSON) { Object json = Tool.parseJSON(Tool.percentDecode(val)); Object newJson = Tool.parseJSON(Tool.percentDecode(value)); if (json != null && json instanceof JSONObject) { Map dictionary = Tool.toMap((JSONObject) json); if (newJson instanceof JSONObject) { Map newDictionary = Tool.toMap((JSONObject) newJson); dictionary.putAll(newDictionary); JSONObject jsonData = new JSONObject(dictionary); formattedParameters.put(key, new Object[] { duplicateParam, makeSubQuery(key, Tool.percentEncode(jsonData.toString())) }); } else { Tool.executeCallback(tracker.getListener(), CallbackType.warning, "Couldn't append value to a dictionary"); } } else if (json != null && json instanceof JSONArray) { try { ArrayList<Object> array = new ArrayList<Object>(); JSONArray jArray = (JSONArray) json; for (int i = 0; i < jArray.length(); i++) { array.add(jArray.get(i).toString()); } if (newJson instanceof JSONArray) { jArray = (JSONArray) newJson; for (int i = 0; i < jArray.length(); i++) { array.add(jArray.get(i).toString()); } JSONObject jsonData = new JSONObject(array.toString()); formattedParameters.put(key, new Object[] { duplicateParam, makeSubQuery(key, Tool.percentEncode(jsonData.toString())) }); } else { Tool.executeCallback(tracker.getListener(), CallbackType.warning, "Couldn't append value to an array"); } } catch (JSONException e) { Tool.executeCallback(tracker.getListener(), CallbackType.warning, "Couldn't append value to an array"); } } else { Tool.executeCallback(tracker.getListener(), CallbackType.warning, "Couldn't append value to a JSON Object"); } } else if (duplicateParam.getType() == Param.Type.JSON) { Tool.executeCallback(tracker.getListener(), CallbackType.warning, "Couldn't append value to a JSON Object"); } else { formattedParameters.put(key, new Object[] { duplicateParam, str + val + p.getOptions().getSeparator() + value }); } } else { formattedParameters.put(key, new Object[] { p, makeSubQuery(key, value) }); } } } return formattedParameters; }