Example usage for org.json.simple JSONArray listIterator

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

Introduction

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

Prototype

public ListIterator<E> listIterator() 

Source Link

Document

Returns a list iterator over the elements in this list (in proper sequence).

Usage

From source file:com.storageroomapp.client.Collections.java

/**
 * Unmarshalls an Collections object from a JSONObject. This method
 * assumes the name-values are immediately attached to the passed object
 * and not nested under a key (e.g. 'array')
 *
 * @param parent the Collections object associated with the json
 * @param jsonObj the JSONObject/*from  w  w  w. j av  a  2 s.co  m*/
 * @return an Collections object, or null if the unpacking failed
 */
static public Collections parseJsonObject(Application parent, JSONObject jsonObj) {
    Collections colls = new Collections(parent);
    colls.url = JsonSimpleUtil.parseJsonStringValue(jsonObj, "@url");
    if (colls.url == null) {
        return null;
    }

    JSONArray collsArray = (JSONArray) jsonObj.get("resources");
    @SuppressWarnings("unchecked")
    Iterator<JSONObject> collsIter = (Iterator<JSONObject>) collsArray.listIterator();
    while (collsIter.hasNext()) {
        JSONObject collJson = collsIter.next();
        Collection collection = Collection.parseJsonObject(parent, collJson);
        if (collection != null) {
            colls.add(collection);
        }
    }
    return colls;
}

From source file:com.worldline.easycukes.commons.helpers.JSONHelper.java

/**
 * Returns <b>true</b> if JSON array a1 is equals to JSON array a2.
 *
 * @param a1 a {@link JSONArray} containing some JSON data
 * @param a2 another {@link JSONArray} containing some JSON data
 * @return true if the json arrays are equals
 *//*from w ww  . j  av  a2  s . com*/
public static boolean equals(@NonNull JSONArray a1, @NonNull JSONArray a2) {
    if (a1 == a2)
        return true;
    if (a1.size() != a2.size())
        return false;

    final ListIterator i1 = a1.listIterator();
    ListIterator i2;
    boolean found = false;
    while (i1.hasNext()) {
        final Object o1 = i1.next();
        found = false;
        i2 = a2.listIterator();
        if (o1 instanceof JSONObject)
            while (i2.hasNext()) {
                final Object o2 = i2.next();
                if (!(o2 instanceof JSONObject))
                    return false;
                if (equals((JSONObject) o1, (JSONObject) o2))
                    found = true;
            }
        else if (o1 instanceof JSONArray)
            while (i2.hasNext()) {
                final Object o2 = i2.next();
                if (!(o2 instanceof JSONArray))
                    return false;
                if (equals((JSONArray) o1, (JSONArray) o2))
                    found = true;
            }
        else
            while (i2.hasNext()) {
                final Object o2 = i2.next();
                if (o1.equals(o2))
                    found = true;
            }
        if (!found)
            return false;
    }
    return true;
}

From source file:com.thesmartweb.swebrank.JSONparsing.java

/**
 * Method to get Bing Search API results
 * @param input the JSON response/*from  w ww . ja va  2 s.c o m*/
 * @param bing_result_number the results number
 * @return an array with the urls of the results
 */
public String[] BingAzureJsonParsing(String input, int bing_result_number) {
    try {
        //Create a parser
        JSONParser parser = new JSONParser();
        //Create the map
        JSONObject jsonmap = (JSONObject) parser.parse(input);
        // Get a set of the entries
        Set set = jsonmap.entrySet();
        Iterator iterator = set.iterator();
        int i = 0;
        while (iterator.hasNext()) {
            Map.Entry entry = (Map.Entry) iterator.next();
            if (entry.getKey().toString().equalsIgnoreCase("d")) {
                JSONObject jsonobject = (JSONObject) entry.getValue();
                JSONArray jsonarray = (JSONArray) jsonobject.get("results");
                Iterator jsonarrayiterator = jsonarray.listIterator();
                while (jsonarrayiterator.hasNext()) {
                    JSONObject linkobject = (JSONObject) jsonarrayiterator.next();
                    links_yahoo_bing[i] = linkobject.get("Url").toString();
                    i++;
                }
            }
        }
        return links_yahoo_bing;
    } catch (ParseException ex) {
        Logger.getLogger(JSONparsing.class.getName()).log(Level.SEVERE, null, ex);
        return links_yahoo_bing;
    }

}

From source file:org.codehaus.cargo.daemon.request.StartRequest.java

/**
 * Gets a list of Properties associated with a key name.
 * /* w w w .j a  v a 2  s  . c o  m*/
 * @param name The key name.
 * @param required If required {@code true}, otherwise {@code false}
 * @return the list of properties.
 */
public List<PropertyTable> getPropertiesList(String name, boolean required) {
    List<PropertyTable> result = new ArrayList<PropertyTable>();

    String value = getParameter(name, required);

    if (value != null && !value.isEmpty()) {
        try {
            JSONArray jsonArray = (JSONArray) JSONValue.parse(value);

            ListIterator iterator = jsonArray.listIterator();
            while (iterator.hasNext()) {
                JSONObject jsonObject = (JSONObject) iterator.next();

                PropertyTable properties = new PropertyTable();
                properties.putAll(jsonObject);

                result.add(properties);
            }
        } catch (Throwable t) {
            throw new CargoDaemonException("Parameter " + name + " is not a JSON array", t);
        }
    }

    return result;
}

From source file:org.opencastproject.index.service.catalog.adapter.AbstractMetadataCollection.java

/**
 * Parse the given JSON string to extract the metadata. The JSON structure must look like this:
 *
 * <pre>/*from w w w.j av a  2 s.  c om*/
 * [
 *  {
 *     "id"        : "field id",
 *     "value"     : "field value",
 *
 *     // The following properties should not be present as they are useless,
 *     // but they do not hurt for the parsing.
 *
 *     "label"     : "EVENTS.SERIES.DETAILS.METADATA.LABEL",
 *     "type"      : "",
 *     // The collection can be a json object like below...
 *     "collection": { "id1": "value1", "id2": "value2" },
 *     // Or a the id of the collection available through the resource endpoint
 *     "collection": "USERS",
 *     "readOnly": false
 *   },
 *
 *   // Additionally fields
 *   ...
 * ]
 * </pre>
 *
 * @param json
 *          A JSON array of metadata as String
 * @throws MetadataParsingException
 *           if the JSON structure is not correct
 * @throws IllegalArgumentException
 *           if the JSON string is null or empty
 */
public AbstractMetadataCollection fromJSON(String json) throws MetadataParsingException {
    if (StringUtils.isBlank(json))
        throw new IllegalArgumentException("The JSON string must not be empty or null!");

    JSONParser parser = new JSONParser();
    JSONArray metadataJSON;
    try {
        metadataJSON = (JSONArray) parser.parse(json);
    } catch (ParseException e) {
        throw new MetadataParsingException("Not able to parse the given string as JSON event metadata.",
                e.getCause());
    }

    ListIterator<JSONObject> listIterator = metadataJSON.listIterator();

    while (listIterator.hasNext()) {
        JSONObject item = listIterator.next();
        String fieldId = (String) item.get(KEY_METADATA_ID);
        MetadataField<?> target = null;

        if (fieldId == null)
            continue;
        Object value = item.get(KEY_METADATA_VALUE);
        if (value == null)
            continue;

        target = outputFields.get(fieldId);
        if (target == null)
            continue;

        target.fromJSON(value);
    }
    return this;
}

From source file:org.opencastproject.index.service.catalog.adapter.MetadataList.java

public void fromJSON(String json) throws MetadataParsingException {
    if (StringUtils.isBlank(json))
        throw new IllegalArgumentException("The JSON string must not be empty or null!");
    JSONParser parser = new JSONParser();
    JSONArray metadataJSON;
    try {// w w w . j  a  v a2  s  .  c o m
        metadataJSON = (JSONArray) parser.parse(json);
    } catch (ParseException e) {
        throw new MetadataParsingException("Not able to parse the given string as JSON metadata list.",
                e.getCause());
    }

    ListIterator<JSONObject> listIterator = metadataJSON.listIterator();

    while (listIterator.hasNext()) {
        JSONObject item = listIterator.next();
        MediaPackageElementFlavor flavor = MediaPackageElementFlavor
                .parseFlavor((String) item.get(KEY_METADATA_FLAVOR));
        String title = (String) item.get(KEY_METADATA_TITLE);
        if (flavor == null || title == null)
            continue;

        JSONArray value = (JSONArray) item.get(KEY_METADATA_FIELDS);
        if (value == null)
            continue;

        Tuple<String, AbstractMetadataCollection> metadata = metadataList.get(flavor.toString());
        if (metadata == null)
            continue;

        metadata.getB().fromJSON(value.toJSONString());
        metadataList.put(flavor.toString(), metadata);
    }
}

From source file:org.opencastproject.index.service.catalog.adapter.MetadataList.java

private void fromJSON(AbstractMetadataCollection metadata, String json) throws MetadataParsingException {
    if (StringUtils.isBlank(json))
        throw new IllegalArgumentException("The JSON string must not be empty or null!");

    JSONParser parser = new JSONParser();
    JSONArray metadataJSON;
    try {//from  w w  w. java  2 s.  com
        metadataJSON = (JSONArray) parser.parse(json);
    } catch (ParseException e) {
        throw new MetadataParsingException("Not able to parse the given string as JSON metadata list.",
                e.getCause());
    }

    ListIterator<JSONObject> listIterator = metadataJSON.listIterator();

    while (listIterator.hasNext()) {
        JSONObject item = listIterator.next();
        String flavor = (String) item.get(KEY_METADATA_FLAVOR);
        String title = (String) item.get(KEY_METADATA_TITLE);
        if (flavor == null || title == null)
            continue;

        JSONArray value = (JSONArray) item.get(KEY_METADATA_FIELDS);
        if (value == null)
            continue;

        metadata.fromJSON(value.toJSONString());
        metadataList.put(flavor, Tuple.tuple(title, metadata));
    }
}

From source file:semRewrite.InterpTest.java

/** **************************************************************
 *///  ww w  .  j a  v  a  2s  . co  m
public static <T> Collection<T> transform(String resourcePath, Function<JSONObject, T> transformer) {
    Collection<T> result = Lists.newArrayList();

    try {
        FileReader fr = new FileReader(new File(resourcePath));
        System.out.println("Reading JSON file: " + resourcePath);
        JSONParser parser = new JSONParser();
        Object obj = parser.parse(fr);
        JSONArray jsonObject = (JSONArray) obj;
        ListIterator<JSONObject> li = jsonObject.listIterator();
        while (li.hasNext()) {
            JSONObject jo = li.next();
            result.add(transformer.apply(jo));
        }
    } catch (Exception e) {
        System.out.println("Parse exception reading: " + resourcePath);
        System.out.println(e.getMessage());
        e.printStackTrace();
        throw new RuntimeException("Parse exception reading: " + resourcePath);
    }
    return result;
}