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:org.openstack.storlet.sbus.ServerSBusInDatagram.java

/**
 * Parses a raw message coming from the wire.
 * The incoming message is constructed by the ClientSBusOutDatagram.
 * The message is structured as follows:
 * Array of file descriptors, already parsed in SBusRawMessage
 * A command related json string of the following structure:
 * {//w  ww .  j  a v a  2  s  .c  o  m
 *     "command": "command encoded as string",
 *     "params": {                            // This element is optional
 *         "key1": "value1",
 *         ...
 *     },
 *     "task_id": "task id encoded as string" // This element is optional
 * }
 * File descriptors metadata, encoded as a JSON array with one
 * element per file descriptor. The i'th element in the array
 * consists of the metadata of the i'th element in the file
 * descriptors array:
 * [
 *     {
 *         "storlets": {
 *             "type": "the fd type encoded as string",  // Mandatory
 *             ... // Additional optional storlets metadata
 *         },
 *         "storage": {
 *             "metadata key1": "metadata value 1",
 *             ...
 *        }
 *     },
 *     ...
 * ]
 * All the values in the above JSON elemens are strings.
 * Once constructed the class provides all necessary accessors to the parsed
 * fields.
 * @param msg   the raw mwssage consisting of the string encoded json formats
 * @see SBusPythonFacade.ClientSBusOutDatagram the python code that serilializes the datagram
 * @see SBusPythonFacade.ServerSBusInDatagram the equivalent python code
 */
public ServerSBusInDatagram(final SBusRawMessage msg) throws ParseException {
    this.fds = msg.getFiles();
    numFDs = this.fds == null ? 0 : this.fds.length;

    JSONObject jsonCmdParams = (JSONObject) (new JSONParser().parse(msg.getParams()));
    this.command = (String) jsonCmdParams.get("command");
    this.params = new HashMap<String, String>();
    if (jsonCmdParams.containsKey("params")) {
        JSONObject jsonParams = (JSONObject) jsonCmdParams.get("params");
        for (Object key : jsonParams.keySet()) {
            this.params.put((String) key, (String) jsonParams.get(key));
        }
    }
    if (jsonCmdParams.containsKey("task_id")) {
        this.taskID = (String) jsonCmdParams.get("task_id");
    }

    String strMD = msg.getMetadata();
    this.metadata = (HashMap<String, HashMap<String, String>>[]) new HashMap[getNFiles()];
    JSONArray jsonarray = (JSONArray) (new JSONParser().parse(strMD));
    Iterator it = jsonarray.iterator();
    int i = 0;
    while (it.hasNext()) {
        this.metadata[i] = new HashMap<String, HashMap<String, String>>();
        HashMap<String, String> storletsMetadata = new HashMap<String, String>();
        HashMap<String, String> storageMetadata = new HashMap<String, String>();
        JSONObject jsonobject = (JSONObject) it.next();
        if (jsonobject.containsKey("storage")) {
            populateMetadata(storageMetadata, (JSONObject) jsonobject.get("storage"));
        }
        if (!jsonobject.containsKey("storlets")) {
        } else {
            populateMetadata(storletsMetadata, (JSONObject) jsonobject.get("storlets"));
        }
        this.metadata[i].put("storage", storageMetadata);
        this.metadata[i].put("storlets", storletsMetadata);
        i++;
    }
}

From source file:org.rexify.eclipse.helper.InternetHelper.java

static public HashMap<String, TemplateModel> getTemplates() {
    HashMap<String, TemplateModel> ret = new HashMap<String, TemplateModel>();
    JSONParser parser = new JSONParser();

    try {//from w  w  w .  j  ava  2 s. c om
        Object obj = parser.parse(GET("http://templates.rexify.org/index.json"));
        JSONArray arr = (JSONArray) obj;

        @SuppressWarnings("rawtypes")
        Iterator iter = arr.iterator();

        while (iter.hasNext()) {
            JSONObject entry = (JSONObject) iter.next();

            System.out.println(entry.get("name"));

            ret.put(entry.get("name").toString(),
                    new TemplateModel(entry.get("name").toString(), entry.get("template").toString()));
        }
    } catch (ParseException ex) {
        // display error message
        System.out.println(ex);
    }

    return ret;
}

From source file:org.sssw.relrel.FactFinder.java

/**
 * Lines 1-3 of the algorithm: init the table with the outgoing links (and
 * find the categories).//  ww  w.  j a  va 2s  .c om
 */
private void scrapInputPage() {

    HttpURLConnection con = null;
    BufferedReader reader = null;

    InputPage = InputPage.replaceAll(" ", "_");

    // do the query and save the retrieved json in an object.
    String queryAddress = String.format("https://%s.%s%s", Locale.ENGLISH, singlePageQuery, InputPage);

    try {

        con = (HttpURLConnection) (new URL(queryAddress)).openConnection();
        con.setRequestProperty("User-Agent", userAgent);
        con.setRequestMethod("GET");
        reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
        Object json = (new JSONParser()).parse(reader);
        // closing connection
        con.disconnect();
        // The retrieved JSON is something like:
        //
        // "query": {
        //        "pages": {
        //            "<PAGE ID NUMBER>": {
        //                "pageid": "<PAGE ID NUMBER>",
        //                "ns": 0,
        //                "title": "<PAGE TITLE>",
        //                "categories": [
        //                    {
        //                        "ns": 14,
        //                        "title": "Category:<CATEGORY 1>"
        //                    },
        //                    {
        //                        "ns": 14,
        //                        "title": "Category:<CATEGORY 2>"
        //                    },
        //                    {
        //                        "ns": 14,
        //                        "title": "Category:<CATEGORY 3>"
        //                    }
        //                ],
        //                "extract":"<TEXT>",
        //                "links": [
        //                    {
        //                        "ns": 0,
        //                        "title": "<LINK 1>"
        //                    },
        //                     {
        //                        "ns": 0,
        //                        "title": "<LINK 2>"
        //                    },
        //                    {
        //                        "ns": 0,
        //                        "title": "<LINK 3>"
        //                    }
        //                 ]
        //            }
        //        }
        //    }
        //}
        // note that NOT ALL the wikis have the "extract" property in the API
        // therefore we may not assume that it will always be there
        JSONObject queryblock = (JSONObject) json;
        JSONObject pagesBlock = (JSONObject) queryblock.get("query");
        JSONObject idBlock = (JSONObject) pagesBlock.get("pages");

        // if we pipe'd more than one title, we'll have more than one pageId entry
        for (Iterator it = idBlock.keySet().iterator(); it.hasNext();) {

            String pageId = (String) it.next();
            JSONObject block = (JSONObject) idBlock.get(pageId);

            // iterate through categories
            JSONArray jsonCats = (JSONArray) block.get("categories");
            if (jsonCats != null) {
                Iterator<JSONObject> iterator = jsonCats.iterator();
                while (iterator.hasNext()) {
                    JSONObject category = (iterator.next());
                    String catName = (String) category.get("title");
                    catName = catName.replaceFirst("Category:", "");
                    catName = catName.replaceFirst("Categoria:", "");
                    if (!catName.toLowerCase().contains("stub") && !catName.contains("Featured Articles")
                            && !catName.toLowerCase().contains("disambiguation")) {

                        if (!this.categories.containsKey(catName) && !blackTerms.contains(catName)) {
                            if (!catName.contains("births") && (!catName.contains("deaths"))) {
                                this.categories.put(catName, 0);
                            }
                        }
                    }
                }
            }

            // We can find related entities in the text
            // many articles have a "See Also" section that begins with
            //          <h2>See also</h2>\n<ul>
            // and ends with:
            //          </ul>
            // To retrieve these links, we don't need to scrap HTML.
            // We can just read the list of links included in the JSON
            // the drawback of this approach is that some pages have huge
            // amounts of links and many of them are uninteresting
            // For example, almost any page has a reference to the
            // definition of ISBN (contained in the references)
            // or of some other kind of wide-used identifier such as:
            // Pub-Med index,
            // Digital-Object-Identifier,
            // International Standard Book Number,
            // Wikisource, and so on.
            JSONArray jsonLinks = (JSONArray) block.get("links");
            if (jsonLinks != null) {
                Iterator<JSONObject> iterator = jsonLinks.iterator();
                while (iterator.hasNext()) {
                    JSONObject link = (iterator.next());
                    String linkname = (String) link.get("title");

                    if (!this.links.containsKey(linkname) && !blackTerms.contains(linkname)) {
                        this.links.put(linkname, 0);
                    }

                }
            }
        }

    } catch (ParseException ex) {
        throw new RuntimeException("Error while parsing JSON by Wikipedia for page: " + InputPage, ex);
    } catch (MalformedURLException ex) {
        throw new RuntimeException("Malformed Wikipedia URL: " + queryAddress, ex);
    } catch (IOException ex) {
        throw new RuntimeException("Error while reading Wikipedia", ex);
    } finally {
        try {
            if (reader != null) {
                reader.close();
            }
        } catch (IOException ex) {
            throw new RuntimeException("Error while closing Wikipedia stream", ex);
        }
    }

}

From source file:org.sssw.relrel.FactFinder.java

private void findFactsInCategory(String cat) {

    HttpURLConnection con = null;
    BufferedReader reader = null;

    System.out.println("Analyzing category : " + cat);

    cat = cat.replaceAll(" ", "_");

    String continueQuery = "";

    do {/*from  ww w.  ja  va 2s.  c o m*/

        // do the query and save the retrieved json in an object.
        String queryAddress = String.format("https://%s.%s%s%s%s", Locale.ENGLISH, categoryQueryBegin,
                continueQuery, categoryQueryEnd, cat);

        try {

            con = (HttpURLConnection) (new URL(queryAddress)).openConnection();
            con.setRequestProperty("User-Agent", userAgent);
            con.setRequestMethod("GET");
            reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
            Object json = (new JSONParser()).parse(reader);
            // closing connection
            con.disconnect();

            JSONObject queryblock = (JSONObject) json;
            JSONObject mainBlock = (JSONObject) queryblock.get("query");
            JSONArray categoriesBlock = (JSONArray) mainBlock.get("categorymembers");

            Iterator<JSONObject> iterator = categoriesBlock.iterator();

            if (continueQuery.isEmpty()) {
                System.out.println("This category has " + categoriesBlock.size() + " pages");
            } else {
                System.out.println("Continuing previous category with " + categoriesBlock.size() + " pages");
            }

            int counter = 0;

            while (iterator.hasNext()) {

                if (counter % 20 == 0)
                    System.out.println("Pages analyzed: " + (counter) + " of " + categoriesBlock.size());

                counter++;

                JSONObject singleCategoryBlock = (iterator.next());
                String pageName = (String) singleCategoryBlock.get("title");
                pageName = pageName.replace(" ", "_");

                // Please be aware that the categories JSON returns not only
                // pages, but also (sub) categories and other things we don't want.
                // So, keep only the pages and skip the rest.
                // For further information, please check
                // https://en.wikipedia.org/wiki/Wikipedia:Namespace
                long pageNamespace = (Long) singleCategoryBlock.get("ns");

                if (!pageName.equals(InputPage) && pageNamespace == 0) {
                    findFactsInPage(pageName);
                }

            }

            // Check if we need to continue
            // But before, reset the continuation id to ensure
            // termination of the do-while loop
            continueQuery = "";

            JSONObject continueBlock = (JSONObject) queryblock.get("query-continue");

            if (continueBlock != null) {
                JSONObject cmBlock = (JSONObject) continueBlock.get("categorymembers");
                continueQuery = (String) cmBlock.get("cmcontinue");
                continueQuery = "&cmcontinue=" + continueQuery;
            }

        } catch (ParseException ex) {
            throw new RuntimeException("Error while parsing JSON by Wikipedia for page: " + cat, ex);
        } catch (MalformedURLException ex) {
            throw new RuntimeException("Malformed Wikipedia URL: " + queryAddress, ex);
        } catch (IOException ex) {
            throw new RuntimeException("Error while reading Wikipedia", ex);
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException ex) {
                throw new RuntimeException("Error while closing Wikipedia stream", ex);
            }
        }
    } while (!continueQuery.isEmpty());

}

From source file:org.sssw.relrel.FactFinder.java

private void findFactsInPage(String pageName) {

    HttpURLConnection con = null;
    BufferedReader reader = null;

    pageName = pageName.replaceAll(" ", "_");

    // do the query and save the retrieved json in an object.
    String queryAddress = String.format("https://%s.%s%s", Locale.ENGLISH, singlePageQuery, pageName);

    try {//from  w  w w.ja  va 2s  .c  o  m

        con = (HttpURLConnection) (new URL(queryAddress)).openConnection();
        con.setRequestProperty("User-Agent", userAgent);
        con.setRequestMethod("GET");
        reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
        Object json = (new JSONParser()).parse(reader);
        // closing connection
        con.disconnect();

        JSONObject queryblock = (JSONObject) json;
        JSONObject pagesBlock = (JSONObject) queryblock.get("query");
        JSONObject idBlock = (JSONObject) pagesBlock.get("pages");

        for (Iterator it = idBlock.keySet().iterator(); it.hasNext();) {

            String pageId = (String) it.next();
            JSONObject block = (JSONObject) idBlock.get(pageId);

            JSONArray jsonLinks = (JSONArray) block.get("links");
            if (jsonLinks != null) {
                Iterator<JSONObject> iterator = jsonLinks.iterator();
                while (iterator.hasNext()) {
                    JSONObject link = (iterator.next());
                    String linkName = (String) link.get("title");

                    if (this.links.containsKey(linkName)) {
                        int newValue = links.get(linkName) + 1;
                        links.replace(linkName, newValue);
                    }

                }
            }
        }

    } catch (ParseException ex) {
        throw new RuntimeException("Error while parsing JSON by Wikipedia for page: " + pageName, ex);
    } catch (MalformedURLException ex) {
        throw new RuntimeException("Malformed Wikipedia URL: " + queryAddress, ex);
    } catch (IOException ex) {
        throw new RuntimeException("Error while reading Wikipedia", ex);
    } finally {
        try {
            if (reader != null) {
                reader.close();
            }
        } catch (IOException ex) {
            throw new RuntimeException("Error while closing Wikipedia stream", ex);
        }
    }

}

From source file:org.surfing.service.things.Manager.java

public static void newSensorData(String deviceName, String data) {
    Device found = null;//from w w w  . j av  a  2s. c  o  m
    for (Device device : Kernel.getInstance().getDevices()) {
        if (device.getName().equals(deviceName)) {
            found = device;
        }
    }
    if (found == null) {
        return;
    }
    JSONObject jsonObject = (JSONObject) JSONValue.parse(data);
    if (jsonObject == null || jsonObject.keySet() == null || jsonObject.keySet().iterator() == null) {
        //System.out.println("Erro json " + data);
        return;
    }

    JSONArray components = (JSONArray) jsonObject.get("components");

    Iterator i = components.iterator();
    while (i.hasNext()) {
        Object oo = i.next();
        JSONObject joo = (JSONObject) oo;
        String thing = joo.get("name").toString();
        String value = joo.get("value").toString();
        found.getThings().get(thing).setLastValue(value);

    }

}

From source file:org.talend.repository.hcatalog.metadata.ExtractMetaDataFromHCatalog.java

/**
 * DOC ycbai Comment method "extractTables".
 * /*from  w  w w  . j a v a  2s. com*/
 * Extract all tables.
 * 
 * @param connection
 * @return
 * @throws Exception
 */
public static synchronized List<TdTable> extractTables(HCatalogConnection connection) throws Exception {
    List<TdTable> tables = new ArrayList<TdTable>();
    if (connection == null) {
        return tables;
    }

    String path = connection.getDatabase() + SEPARATOR + TABLE;
    WebClient client = HCatalogServiceUtil.getHCatalogClient(connection, path);
    JSONObject obj = HCatalogServiceUtil.getDataFromHCatalog(client);
    JSONArray tableArray = (JSONArray) obj.get(TABLES);
    if (tableArray != null) {
        Iterator iterator = tableArray.iterator();
        while (iterator.hasNext()) {
            Object object = iterator.next();
            if (object != null && object instanceof String) {
                String tableName = (String) object;
                TdTable table = HCatalogSchemaUtil.createDefaultTable(tableName);
                tables.add(table);
            }
        }
    }

    return tables;
}

From source file:org.talend.repository.hcatalog.metadata.ExtractMetaDataFromHCatalog.java

/**
 * DOC ycbai Comment method "extractColumns".
 * /*  w ww  . ja va  2s . c  om*/
 * Extract all columns contains partition columns.
 * 
 * @param connection
 * @param tableName
 * @return
 * @throws Exception
 */
public static synchronized List<MetadataColumn> extractColumns(HCatalogConnection connection, String tableName)
        throws Exception {
    List<MetadataColumn> columns = new ArrayList<MetadataColumn>();
    List<String> exisColumnNames = new ArrayList<String>();
    if (connection == null) {
        return columns;
    }

    String path = connection.getDatabase() + SEPARATOR + TABLE + SEPARATOR + tableName;
    WebClient client = HCatalogServiceUtil.getHCatalogClient(connection, path);
    JSONObject obj = HCatalogServiceUtil.getDataFromHCatalog(client);
    JSONArray columnArray = (JSONArray) obj.get(COLUMNS);
    if (columnArray != null) {
        Iterator iterator = columnArray.iterator();
        while (iterator.hasNext()) {
            Object object = iterator.next();
            if (object != null && object instanceof JSONObject) {
                JSONObject columnObj = (JSONObject) object;
                Object nameObj = columnObj.get(NAME);
                Object typeObj = columnObj.get(TYPE);
                if (nameObj != null && nameObj instanceof String && typeObj != null
                        && typeObj instanceof String) {
                    String columnName = (String) nameObj;
                    String columnType = (String) typeObj;
                    MetadataColumn metadataColumn = ConnectionFactory.eINSTANCE.createMetadataColumn();
                    JavaType talendType = JavaTypesManager.getJavaTypeFromName(columnType);
                    if (talendType == null) {
                        talendType = JavaTypesManager.STRING;
                    }
                    metadataColumn.setTalendType(talendType.getId());
                    if (metadataColumn.getTalendType().equals(JavaTypesManager.DATE.getId())) {
                        metadataColumn.setPattern(DATE_FORMAT);
                    }
                    String columnLabel = IndiceHelper.getIndexedLabel(columnName, exisColumnNames);
                    metadataColumn.setLabel(columnLabel);
                    if (!exisColumnNames.contains(columnLabel)) {
                        exisColumnNames.add(columnLabel);
                    }
                    columns.add(metadataColumn);
                }
            }
        }
    }

    return columns;
}

From source file:org.talend.repository.hcatalog.metadata.ExtractMetaDataFromHCatalog.java

/**
 * DOC ycbai Comment method "extractPartitionsByJsonStr".
 * //from ww  w  .jav a2 s . co m
 * Extract partition columns from json string.
 * 
 * @param jsonString
 * @return
 */
public static synchronized List<MetadataColumn> extractPartitionsByJsonStr(String jsonString) {
    List<MetadataColumn> columns = new ArrayList<MetadataColumn>();
    List<String> exisColumnNames = new ArrayList<String>();
    if (jsonString == null) {
        return columns;
    }

    JSONObject jsonObject = (JSONObject) JSONValue.parse(jsonString);
    JSONArray partitionArray = (JSONArray) jsonObject.get(PARTITIONS);
    if (partitionArray != null) {
        Iterator iterator = partitionArray.iterator();
        while (iterator.hasNext()) {
            Object object = iterator.next();
            if (object != null && object instanceof JSONObject) {
                JSONObject partitionObj = (JSONObject) object;
                JSONArray valuesArray = (JSONArray) partitionObj.get(VALUES);
                if (valuesArray != null) {
                    Iterator valsIterator = valuesArray.iterator();
                    while (valsIterator.hasNext()) {
                        Object valsObj = valsIterator.next();
                        if (valsObj instanceof JSONObject) {
                            JSONObject columnObj = (JSONObject) valsObj;
                            Object nameObj = columnObj.get(COLUMN_NAME);
                            Object valueObj = columnObj.get(COLUMN_VALUE);
                            if (nameObj != null && nameObj instanceof String && valueObj != null
                                    && valueObj instanceof String) {
                                String columnName = (String) nameObj;
                                // String columnValue = (String) valueObj;
                                if (exisColumnNames.contains(columnName)) {
                                    continue;
                                } else {
                                    exisColumnNames.add(columnName);
                                }
                                MetadataColumn metadataColumn = ConnectionFactory.eINSTANCE
                                        .createMetadataColumn();
                                metadataColumn.setTalendType(JavaTypesManager.STRING.getId());
                                metadataColumn.setLabel(columnName);
                                columns.add(metadataColumn);
                            }
                        }
                    }
                }
            }
        }
    }

    return columns;
}

From source file:org.talend.repository.hcatalog.metadata.ExtractMetaDataFromHCatalog.java

public static synchronized String extractPartitionNameByJsonStr(String jsonString) {
    if (jsonString == null) {
        return null;
    }//from  w w  w .  ja  v  a2 s .  c  o  m

    JSONObject jsonObject = (JSONObject) JSONValue.parse(jsonString);
    JSONArray partitionArray = (JSONArray) jsonObject.get(PARTITIONS);
    if (partitionArray != null) {
        Iterator<Object> partitionIterator = partitionArray.iterator();
        while (partitionIterator.hasNext()) {
            Object partitionsObj = partitionIterator.next();
            if (partitionsObj != null && partitionsObj instanceof JSONObject) {
                JSONObject partitions = (JSONObject) partitionsObj;
                Object partitionName = partitions.get(NAME);
                return partitionName == null ? "" : String.valueOf(partitionName); //$NON-NLS-1$
            }
        }
    }

    return null;
}