Example usage for org.json.simple JSONArray set

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

Introduction

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

Prototype

public E set(int index, E element) 

Source Link

Document

Replaces the element at the specified position in this list with the specified element.

Usage

From source file:bhl.pages.handler.PagesGetHandler.java

protected static void sortList(JSONArray a) {
    int increment = a.size() / 2;
    while (increment > 0) {
        for (int i = increment; i < a.size(); i++) {
            int j = i;
            JSONObject temp = (JSONObject) a.get(i);
            int tempN = ((Number) temp.get("n")).intValue();
            JSONObject aObj = ((JSONObject) a.get(j - increment));
            int aN = ((Number) aObj.get("n")).intValue();
            while (j >= increment && aN > tempN) {
                a.set(j, a.get(j - increment));
                j = j - increment;//from w w w  .j  a v  a2 s  .co m
                if (j >= increment) {
                    aObj = ((JSONObject) a.get(j - increment));
                    aN = ((Number) aObj.get("n")).intValue();
                }
            }
            a.set(j, temp);
        }
        if (increment == 2) {
            increment = 1;
        } else {
            increment *= (5.0 / 11);
        }
    }
}

From source file:com.valygard.aohruthless.utils.config.JsonConfiguration.java

/**
 * Writes a JSONArray to memory at a specified String key.
 * //from  ww w. ja  v  a 2s.c o  m
 * @param key
 *            the String pathway
 * @param array
 *            the JSONArray
 * @return the JSONConfiguration instance
 */
@SuppressWarnings("unchecked")
public JsonConfiguration writeArray(String key, JSONArray array) {
    for (Object value : array) {
        if (value == null)
            continue;
        array.set(array.indexOf(value), JSONValue.toJSONString(value));
    }
    return writeObject(key, array);
}

From source file:com.fujitsu.dc.test.jersey.ODataCommon.java

/**
 * Json??results?./*from  w w  w. j a v  a  2  s. co  m*/
 * @param json JSONObject
 * @param locationHeader LOCATION
 * @param type ?
 * @param additional ???
 * @param np NP???
 * @param etag etag???
 * @param published published???
 */
@SuppressWarnings("unchecked")
public static final void checkResults(final JSONObject json, final String locationHeader, final String type,
        final Map<String, Object> additional, Map<String, String> np, String etag, String published) {
    String value;

    // d:__published?????????
    value = (String) json.get("__published");
    assertNotNull(value);
    assertTrue(validDate(value));

    // d:__published??????????
    if (published != null) {
        assertEquals(published, value);
    }

    // d:__updated?????????
    value = (String) json.get("__updated");
    assertNotNull(value);
    assertTrue(validDate(value));

    // __metadata?
    JSONObject metadata = (JSONObject) json.get("__metadata");
    // uri?
    value = (String) metadata.get("uri");
    assertNotNull(value);
    if (locationHeader != null) {
        // LOCATION?????
        assertEquals(locationHeader, value);
    }
    // etag?
    value = (String) metadata.get("etag");
    if (etag != null) {
        assertEquals(etag, value);
    }

    // type?
    value = (String) metadata.get("type");
    assertNotNull(value);
    assertEquals(type, value);

    if (additional != null) {
        // ??
        for (Map.Entry<String, Object> e : additional.entrySet()) {
            Object jsonValue = json.get(e.getKey());
            if (jsonValue instanceof Integer) {
                jsonValue = ((Integer) jsonValue).longValue();
            }
            if (jsonValue instanceof JSONArray) {
                JSONArray array = ((JSONArray) jsonValue);
                for (int i = 0; i < array.size(); i++) {
                    if (array.get(i) instanceof Integer) {
                        array.set(i, ((Integer) array.get(i)).longValue());
                    }
                }
            }
            Object expected = e.getValue();
            if (expected instanceof Integer) {
                expected = ((Integer) expected).longValue();
            }
            if (expected instanceof JSONArray) {
                JSONArray array = ((JSONArray) expected);
                for (int i = 0; i < array.size(); i++) {
                    if (array.get(i) instanceof Integer) {
                        array.set(i, ((Integer) array.get(i)).longValue());
                    }
                }
            }
            assertEquals(expected, jsonValue);
        }
    }

    if (np != null) {
        // NP???
        for (Map.Entry<String, String> e : np.entrySet()) {
            JSONObject jsonValue = (JSONObject) json.get(e.getKey());
            JSONObject defferedValue = (JSONObject) jsonValue.get("__deferred");
            String uriValue = (String) defferedValue.get("uri");
            assertEquals(e.getValue(), uriValue);
        }
    }
}

From source file:es.alrocar.jpe.writer.handler.MiniJPEWriterHandler.java

/**
 * {@inheritDoc}/*from  w  w  w .j  a  v  a2s.  co m*/
 */
public Object endPoint(Object point, String from, String to) {
    JSONArray coords = ((JSONArray) ((LinkedHashMap) point).get("coordinates"));

    try {
        double[] xy = GeotoolsUtils.transform(from, to, new double[] {
                Double.valueOf(String.valueOf(coords.get(0))), Double.valueOf(String.valueOf(coords.get(1))) });
        if (xy != null) {
            coords.set(0, xy[0]);
            coords.set(1, xy[1]);
            ((LinkedHashMap) point).put("coordinates", coords);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return point;
}

From source file:importer.handler.post.stages.SAXSplitter.java

/**
 * Sort an array of path objects// www . j a v  a2s. c  o m
 * @param jArr the array of path json objects
 * @return the sorted array
 */
JSONArray sort(JSONArray jArr) {
    int increment = jArr.size() / 2;
    while (increment > 0) {
        for (int i = increment; i < jArr.size(); i++) {
            int j = i;
            JSONObject temp = (JSONObject) jArr.get(i);
            while (j >= increment && compare((JSONObject) jArr.get(j - increment), temp) > 0) {
                jArr.set(j, jArr.get(j - increment));
                j = j - increment;
            }
            jArr.set(j, temp);
        }
        if (increment == 2)
            increment = 1;
        else
            increment *= (5.0 / 11);
    }
    return jArr;
}

From source file:de.fhg.fokus.odp.middleware.ckan.CKANGatewayUtil.java

/**
 * Returns a list of the most popular tags.
 * /*from  w w w. j  av a  2s  .c om*/
 * @param numberOfTags
 *            the number of popular tags to return.
 * @return the most popular tags or null if an error occurred.
 */
@Deprecated
@SuppressWarnings("unchecked")
public static JSONArray getMostPopularTagsOld(int numberOfTags) {

    // check the parameters
    if (numberOfTags <= 0) {
        return null;
    }

    // the json array to return
    JSONArray toreturn = new JSONArray();

    // the array to store rating values
    double[] ratingValues = new double[numberOfTags];
    // even tough Java does it automatically, we set the values to zero
    for (int i = 0; i < ratingValues.length; i++) {
        ratingValues[i] = 0.0;
    }

    // a help array for the package ratings
    HashMap<String, Double> packageRatings = new HashMap<String, Double>();

    // prepare the REST API call
    String RESTcall = "api/rest/tag";

    try {
        // run the REST call to obtain all tags
        String tagListString = connectorInstance.restCallWithAuthorization(RESTcall, null);
        if (tagListString == null) {
            log.log(Level.SEVERE, "Failed to realize api call \"" + url + RESTcall + "\" !!!");
            return null;
        }

        // parse the JSON string and obtain an array of JSON objects
        Object obj = JSONValue.parse(tagListString);
        JSONArray array = (JSONArray) obj;

        // move over the tags in the array
        for (int i = 0; i < array.size(); i++) {

            // pick the tag name
            String tagName = (String) array.get(i);

            // run the REST call to obtain all packages for the tag in
            // question
            String tRESTcall = RESTcall + "/" + tagName;
            String pListString = connectorInstance.restCallWithAuthorization(tRESTcall, null);

            if (pListString == null) {
                log.log(Level.SEVERE, "Failed to realize api call \"" + url + RESTcall + "\" !!!");
                return null;
            }

            // parse the JSON string
            Object pObj = JSONValue.parse(pListString);
            JSONArray pArray = (JSONArray) pObj;

            // iterate over the JSON array
            double tagRating = 0.0;
            for (int j = 0; j < pArray.size(); j++) {
                // pick the name of the package
                String pName = (String) (pArray.get(j));

                // check whether the average rating value has already been
                // obtained
                Double aRatingValueD = packageRatings.get(pName);

                if (aRatingValueD == null) {
                    // in case it was not obtained until now --> get it and
                    // store it locally
                    double aRatingValue = CKANGatewayUtil.getDataSetRatingsAverage(pName);
                    aRatingValueD = new Double(aRatingValue);
                    packageRatings.put(pName, aRatingValueD);
                }

                // update the tag rating
                tagRating += aRatingValueD.doubleValue();
            }

            // update the toreturn array
            if (toreturn.size() < numberOfTags) {
                toreturn.add(tagName);
                ratingValues[toreturn.size() - 1] = tagRating;

            } else {
                // get the smallest value in the rating values
                int minIndex = minDoubleArray(ratingValues);
                if (ratingValues[minIndex] < tagRating) {
                    ratingValues[minIndex] = tagRating;
                    toreturn.set(minIndex, tagName);
                }
            }
        }
    }
    // catch potential exceptions
    catch (MalformedURLException e) {
        log.log(Level.SEVERE, "Failed to realize api call \"" + url + RESTcall + "\" !!!");
        return null;
    } catch (IOException e) {
        return null;
    }

    return toreturn;
}

From source file:edu.vt.vbi.patric.cache.DataLandingGenerator.java

private JSONObject getTop5List(String type) {
    JSONObject jsonData = null;//from w  w  w. j a va2  s.  c  om

    try {
        Map facets = dataApi.getFieldFacets(SolrCore.GENOME, "*:*", null, type);
        Map status = (Map) ((Map) facets.get("facets")).get(type);

        if (!status.isEmpty()) {
            JSONArray data = new JSONArray();
            long cntTop = 0, cntSecond = 0;

            int i = 0;
            for (Map.Entry<String, Integer> entry : (Iterable<Map.Entry>) status.entrySet()) {
                String key = entry.getKey();
                int count = entry.getValue();

                JSONObject item = new JSONObject();

                String icon;
                switch (key) {
                case "Human, Homo sapiens":
                    icon = "/patric/images/hosts/human.png";
                    break;
                case "Cattle, Bos sp.":
                    icon = "/patric/images/hosts/cow.png";
                    break;
                case "Cattle, Bos taurus":
                    icon = "/patric/images/hosts/cow.png";
                    break;
                case "Bovine":
                    icon = "/patric/images/hosts/cow.png";
                    break;
                case "Pig, Sus scrofa domesticus":
                    icon = "/patric/images/hosts/pig.png";
                    break;
                case "Pig, Sus scrofa":
                    icon = "/patric/images/hosts/pig.png";
                    break;
                case "Cassava, Manihot esculenta":
                    icon = "/patric/images/hosts/cassava.png";
                    break;
                case "Bovine, Bovinae":
                    icon = "/patric/images/hosts/cow.png";
                    break;
                case "Cow, Bos taurus":
                    icon = "/patric/images/hosts/cow.png";
                    break;
                case "American bison, Bison bison":
                    icon = "/patric/images/hosts/bison.png";
                    break;
                case "Mouse, Mus musculus":
                    icon = "/patric/images/hosts/mouse.png";
                    break;
                case "cow":
                    icon = "/patric/images/hosts/cow.png";
                    break;
                case "pig":
                    icon = "/patric/images/hosts/pig.png";
                    break;
                case "tick":
                    icon = "/patric/images/hosts/tick.png";
                    break;
                case "sheep":
                    icon = "/patric/images/hosts/sheep.png";
                    break;
                case "Chicken, Gallus gallus":
                    icon = "/patric/images/hosts/blank.png";
                    break;
                case "USA":
                    icon = "/patric/images/flags/United-States.png";
                    break;
                default:
                    icon = "/patric/images/flags/" + key.replaceAll(" ", "-") + ".png";
                    break;
                }

                item.put("icon", icon);
                item.put("label", key);
                item.put("m_label", key.replaceAll(" ", "_").toLowerCase());
                item.put("value", count);

                data.add(item);
                //
                if (i == 0) {
                    cntTop = count;
                } else if (i == 1) {
                    cntSecond = count;
                } else if (i == 4) {
                    break;
                }
                i++;
            }
            // reported?
            if (cntTop > 2 * cntSecond) {
                JSONObject item = (JSONObject) data.get(0);
                item.put("reported", Math.round(cntSecond * 1.5));
                data.set(0, item);
            }
            jsonData = new JSONObject();
            if (type.equals("host_name")) {
                jsonData.put("chart_title", "Bacterial Host");
                jsonData.put("chart_desc", "Top 5 Bacterial Hosts");
                jsonData.put("tab_title", "Host");
            } else if (type.equals("isolation_country")) {
                jsonData.put("chart_title", "Isolation Country");
                jsonData.put("chart_desc", "Top 5 Isolation Countries");
                jsonData.put("tab_title", "Isolation Country");
            }
            jsonData.put("data", data);
        }
    } catch (IOException e) {
        LOGGER.error(e.getMessage(), e);
    }

    return jsonData;
}