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:luceneprueba.Index.java

public void create() {
    try {/*from  w ww .  j ava  2 s  . c  o  m*/
        // Storing index in disk
        IndexWriterConfig config = new IndexWriterConfig(analyzer);
        try (IndexWriter iwriter = new IndexWriter(directory, config)) {
            Document doc;
            File dir = new File("files/input");
            File[] files = dir.listFiles();

            if (files == null) {
                //System.out.println("No existe la carpeta \'input\' dentro de la carpeta files.");
                return;
            }

            if (files.length == 0) {
                //System.out.println("No hay ningun archivo en la carpeta \'input\' para ser indexado");
                return;
            }

            BufferedReader br;
            String fileContent;
            JSONArray jsonArray;
            JSONParser jsonParser = new JSONParser();
            for (File file : files) {
                if (file.isFile() && file.canRead() && file.getName().endsWith(".txt")) {
                    //System.out.println("Indexando el archivo: "+file.getName());

                    br = new BufferedReader(new FileReader(file));
                    fileContent = br.readLine();
                    jsonArray = (JSONArray) jsonParser.parse(fileContent);
                    Iterator i = jsonArray.iterator();
                    while (i.hasNext()) {
                        JSONObject json = (JSONObject) i.next();
                        doc = new Document();
                        doc.add(new TextField("movieId", file.getName().split(".txt")[0], Field.Store.YES));
                        doc.add(new TextField("path", file.toString(), Field.Store.YES));
                        doc.add(new TextField("title", (String) json.get("Title"), Field.Store.YES));
                        doc.add(new TextField("score", (String) json.get("Score"), Field.Store.YES));
                        doc.add(new TextField("review", (String) json.get("Review"), Field.Store.YES));
                        doc.add(new TextField("date", (String) json.get("Date"), Field.Store.YES));
                        doc.add(new TextField("genre", (String) json.get("Genre"), Field.Store.YES));
                        doc.add(new TextField("score", (String) json.get("Score"), Field.Store.YES));
                        iwriter.addDocument(doc);
                    }
                    br.close();
                }
            }
            iwriter.close();
        }
    } catch (IOException | ParseException e) {
        e.printStackTrace();
    }
}

From source file:it.uniud.ailab.dcore.annotation.annotators.WikipediaInferenceAnnotator.java

/**
 * Fill the hypernyms and related link maps by querying Wikipedia. The hypernyms
 * map contains the categories found for every page, while the related map
 * contains the related links./*from  w w  w  .  j av a2  s  .  com*/
 * 
 * @param grams the grams to analyze.
 */
private void findHyperymsAndRelated(List<Keyphrase> grams) {

    HttpURLConnection con = null;
    BufferedReader reader = null;

    // We may pipe several article titles in one query, but for some awkward reason,
    // the API won't give us the full category list of the requested terms, nor the full definition
    for (Keyphrase currentGram : grams) {

        // this will contain the categories (i.e. our hypernyms)s
        ArrayList<String> wikiCategories = new ArrayList<>();

        // this will contain the related links
        ArrayList<String> wikiLinks = new ArrayList<>();

        String page = ((UriAnnotation) currentGram.getAnnotation(WIKIURI)).getUriTitle();

        /*
        // get the correct annotation that generated the wikiflag
        TextAnnotation a = (TextAnnotation) currentGram.getTokens().get(0).
            getAnnotation(WIKIFLAG);
                
        // the annotations have the same length, so we may have a legit
        // wikipedia surface as the gram
                
        if (a.getTokens().length == currentGram.getTokens().size()) {
                
        boolean isTagged = true;
                
        for (int i = 0; i < a.getTokens().length && isTagged; i++) {
            isTagged = a.getTokens()[i].equals(
                    currentGram.getTokens().get(i));
        }
                
        if (isTagged) {
            page = a.getAnnotation();
        }
        }*/

        if (page == null)
            throw new AnnotationException(this, "I couldn't find the correct annotation.");

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

        // do the query and save the retrieved json in an object.
        String queryAddress = String.format("https://%s.%s%s", componentLocale.getLanguage(), wikipediaQuery,
                page);

        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);
                // finally... The Categories!
                JSONArray categories = (JSONArray) block.get("categories");
                if (categories != null) {
                    Iterator<JSONObject> iterator = categories.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")) {
                            //System.out.println(catName);
                            if (!wikiCategories.contains(catName) && !blackTerms.contains(catName)) {
                                wikiCategories.add(catName);
                            }
                        }
                    }
                }

                // 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 links = (JSONArray) block.get("links");
                if (links != null) {
                    Iterator<JSONObject> iterator = links.iterator();
                    while (iterator.hasNext()) {
                        JSONObject link = (iterator.next());
                        String linkname = (String) link.get("title");

                        if (!wikiLinks.contains(linkname) && !blackTerms.contains(linkname)) {
                            wikiLinks.add(linkname);
                        }

                    }
                }
            }

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

        // Update the results.

        // How does it work? The strenght of an hypernym or related concept
        // is the sum of all the scores of the KPs which generate it.

        // So, for example, if the KPs "Software" and "Engineering" have
        // both score 0.5, and both have the related link "Software Engineering", 
        // the strength of the "Software Engineering" related concept is 
        // going to be 1.

        for (String cat : wikiCategories) {

            if (hypernyms.containsKey(cat)) {
                hypernyms.replace(cat, hypernyms.get(cat) + currentGram.getFeature(SCORE));
            } else {
                hypernyms.put(cat, currentGram.getFeature(SCORE));
            }

        }

        for (String rel : wikiLinks) {
            if (related.containsKey(rel)) {
                related.replace(rel, related.get(rel) + currentGram.getFeature(SCORE));
            } else
                related.put(rel, currentGram.getFeature(SCORE));
        }

    } // for (Gram currentGram : grams)
}

From source file:de.ingrid.external.gemet.GEMETService.java

@Override
public TreeTerm[] getHierarchyNextLevel(String url, String termId, Locale locale) {
    if (termId == null) {
        return getHierarchyTopLevel(locale);
    }//from  w  ww. j a v  a2  s. c  om

    if (termId.trim().length() == 0) {
        log.warn("No termId passed (" + termId + "), we return empty result !");
        return new TreeTerm[] {};
    }

    String language = getGEMETLanguageFilter(locale);

    // get concept itself, this is the parent
    JSONObject parent = gemetClient.getConceptAsJSON(termId, language);
    // we check on null, cause some concepts are buggy in service !
    // (e.g. concept/15041)
    if (parent == null) {
        log.error("Problems fetching " + termId + " we return empty children list !");
        return new TreeTerm[] {};
    }

    JSONArray parentArray = JSONUtils.toJSONArray(parent);

    // get direct children
    List<JSONArray> childrenList = gemetClient.getChildConcepts(termId, language);

    // get children of children (next hierarchy level) and create TreeTerms
    List<TreeTerm> resultList = new ArrayList<TreeTerm>();
    for (JSONArray children : childrenList) {
        Iterator<JSONObject> childrenIterator = children.iterator();
        while (childrenIterator.hasNext()) {

            // map basic TreeTerm
            TreeTerm resultTreeTerm = gemetMapper.mapToTreeTerm(childrenIterator.next(), null, null);

            // add parent to TreeTerm
            gemetMapper.addParentsToTreeTerm(resultTreeTerm, parentArray);

            // get next hierarchy level (subchildren) and add to TreeTerm !
            // This is time consuming, we only do this for terms where we do
            // not know whether there are children !
            // For GROUPS OR SOUPERGROUPS we just add DUMMY CHILD to
            // indicate children, so we reduce requests !
            if (TermType.NODE_LABEL.equals(resultTreeTerm.getType())) {
                // set DUMMY CHILD to indicate children
                resultTreeTerm.addChild(new TreeTermImpl());

            } else {
                List<JSONArray> subChildrenList = gemetClient.getChildConcepts(resultTreeTerm.getId(),
                        language);
                for (JSONArray subChildren : subChildrenList) {
                    gemetMapper.addChildrenToTreeTerm(resultTreeTerm, subChildren);
                }
            }

            resultList.add(resultTreeTerm);
        }
    }

    return resultList.toArray(new TreeTerm[resultList.size()]);
}

From source file:org.exfio.weave.storage.StorageContext.java

public String[] getCollectionIds(URI location) throws WeaveException, NotFoundException {
    Log.getInstance().debug("getCollectionIds()");

    List<String> ids = new LinkedList<String>();

    //Get JSONUtils payload and extract JSONUtils array
    JSONObject jsonTmp = getJSONPayload(location, true);
    JSONArray jsonArray = (JSONArray) jsonTmp.get(null);

    //Iterate through jsonArray and build list of ids
    try {/*w ww  .  j a va  2 s  .  c o m*/
        @SuppressWarnings("unchecked")
        Iterator<String> iterator = jsonArray.iterator();
        while (iterator.hasNext()) {
            ids.add((String) iterator.next());
        }
    } catch (ClassCastException e) {
        throw new WeaveException(e);
    }

    return ids.toArray(new String[0]);
}

From source file:org.exfio.weave.storage.StorageContext.java

public WeaveBasicObject[] getCollection(URI location) throws WeaveException, NotFoundException {
    Log.getInstance().debug("getCollection()");

    List<WeaveBasicObject> listWbo = new LinkedList<WeaveBasicObject>();

    //Get JSONUtils payload and extract JSONUtils array
    JSONObject jsonTmp = getJSONPayload(location, true);
    JSONArray jsonArray = (JSONArray) jsonTmp.get(null);

    //Iterate through jsonArray and build WBOs
    try {//from w  ww  . j ava 2  s. c om
        @SuppressWarnings("unchecked")
        Iterator<JSONObject> iterator = jsonArray.iterator();
        while (iterator.hasNext()) {
            JSONObject jsonObject = (JSONObject) iterator.next();

            String id = (String) jsonObject.get("id");
            Double modified = JSONUtils.toDouble(jsonObject.get("modified"));
            Long sortindex = (Long) jsonObject.get("sortindex");
            String payload = (String) jsonObject.get("payload");
            Long ttl = (Long) jsonObject.get("ttl");

            listWbo.add(new WeaveBasicObject(id, modified, sortindex, ttl, payload));
        }
    } catch (ClassCastException e) {
        throw new WeaveException(e);
    }

    return listWbo.toArray(new WeaveBasicObject[0]);
}

From source file:assignment3.Populate.java

CreateUser() {
    try {/*from w  w  w  . ja v  a 2 s.c  o  m*/

        Class.forName("oracle.jdbc.driver.OracleDriver");

    } catch (ClassNotFoundException e) {

        System.out.println("JDBC Driver Missing");
        e.printStackTrace();
        return;

    }

    System.out.println("Oracle JDBC Driver Connected");

    Connection conn = null;

    try {

        conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "MAYUR", "123456");
        conn.setAutoCommit(false);

        PreparedStatement ps = conn
                .prepareStatement("insert into yelp_user values (?, ?, ?,?, ?, ?,?, ?, ?,?,?)");

        JSONParser parser = new JSONParser();
        Object obj = parser.parse(new BufferedReader(new FileReader(
                "C:\\Users\\mayur\\Downloads\\YelpDataset\\YelpDataset-CptS451\\yelp_user.json")));

        //Object obj = parser.parse(new BufferedReader(new FileReader("C:\\Users\\mayur\\Downloads\\YelpDataset\\YelpDataset-CptS451\\yelp_user1.json")));

        JSONArray jsonArray = (JSONArray) (obj);

        // JSONArray jsonArray = (JSONArray)(obj1);
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject t = (JSONObject) jsonArray.get(i);

            String c = t.get("yelping_since").toString();

            Date yelping_since = (Date) java.sql.Date.valueOf(c + "-01");

            JSONObject votes = (JSONObject) t.get("votes"); // get all votes details
            Long votes_funny = (Long) votes.get("funny");
            Long votes_useful = (Long) votes.get("useful");
            Long votes_cool = (Long) votes.get("cool");

            Long review_count = (Long) t.get("review_count");
            String name = t.get("name").toString();
            String user_id = t.get("user_id").toString();

            JSONArray friends = (JSONArray) (t).get("friends");
            int numfriends = 0;
            if (friends != null) {
                Iterator<String> iterator = friends.iterator();
                ArrayList<String> friendid_list = new ArrayList<String>();

                while (iterator.hasNext()) {
                    friendid_list.add(iterator.next());
                }

                if (friendid_list != null)
                    numfriends = friendid_list.size();

                friendid_list = null;
                iterator = null;

            }
            Long fans = (Long) t.get("fans");
            double average_stars = (double) t.get("average_stars");
            String type = t.get("type").toString();

            ps.setDate(1, yelping_since);
            ps.setLong(2, votes_funny);
            ps.setLong(3, votes_useful);
            ps.setLong(4, votes_cool);
            ps.setLong(5, review_count);
            ps.setString(6, name);
            ps.setString(7, user_id);
            ps.setLong(8, fans);
            ps.setDouble(9, average_stars);
            ps.setString(10, type);
            ps.setInt(11, numfriends);

            ps.executeUpdate();
            System.out.println("Record inserted " + i);

        }

        conn.commit();

        ps.close();

    } catch (Exception e) {

        System.out.println("Connection Failed! Check output console");
        e.printStackTrace();
        return;

    }

}

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

@Override
public ApiResponse getActiveOrders() {
    ApiResponse apiResponse = new ApiResponse();
    String url = API_BASE_URL + "/" + API_ORDERS + "/" + API_MY_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();
        JSONArray orders = (JSONArray) httpAnswerJson.get("orders");
        for (Iterator<JSONObject> order = orders.iterator(); order.hasNext();) {
            orderList.add(parseOrder(order.next(), null));
        }//from  w w w .j a v a  2  s .  com
        apiResponse.setResponseObject(orderList);
    } else {
        apiResponse = response;
    }
    return apiResponse;
}

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

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

    args.put("market", pair.toStringSepSpecial("/").toUpperCase());

    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  ww  w  .  j a v  a  2  s.com*/
        apiResponse.setResponseObject(orderList);
    } else {
        apiResponse = response;
    }

    return apiResponse;
}

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

private ApiResponse getLastTradesImpl(CurrencyPair pair, long startTime) {
    ApiResponse apiResponse = new ApiResponse();
    String url = API_BASE_URL + "/" + API_ORDERS + "/" + API_MY_HISTORY;
    HashMap<String, String> args = new HashMap<>();
    boolean isGet = false;
    ArrayList<Trade> tradeList = new ArrayList<>();

    args.put("market", pair.toStringSepSpecial("/"));

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

From source file:com.hypersocket.client.HypersocketClient.java

protected boolean parseLogonJSON(String json, Map<String, String> params, List<Prompt> prompts)
        throws ParseException, IOException {

    JSONParser parser = new JSONParser();
    prompts.clear();//from  w  w w  .  jav a  2 s.co m

    JSONObject result = (JSONObject) parser.parse(json);
    if (!(Boolean) result.get("success")) {
        JSONObject template = (JSONObject) result.get("formTemplate");
        JSONArray fields = (JSONArray) template.get("inputFields");
        Boolean lastResultSuccessfull = (Boolean) result.get("lastResultSuccessfull");
        @SuppressWarnings("unchecked")
        Iterator<JSONObject> it = (Iterator<JSONObject>) fields.iterator();
        while (it.hasNext()) {
            JSONObject field = it.next();

            PromptType type = PromptType.TEXT;
            try {
                type = PromptType.valueOf(field.get("type").toString().toUpperCase());
            } catch (IllegalArgumentException iae) {
                log.warn("Unknown prompt type, default to text.", iae);
            }

            switch (type) {
            case HIDDEN: {
                params.put((String) field.get("resourceKey"), (String) field.get("defaultValue"));
                break;
            }
            case SELECT: {
                Prompt p = new Prompt(type, (String) field.get("resourceKey"),
                        (String) field.get("defaultValue"));
                JSONArray options = (JSONArray) field.get("options");
                @SuppressWarnings("unchecked")
                Iterator<JSONObject> it2 = (Iterator<JSONObject>) options.iterator();
                while (it2.hasNext()) {
                    JSONObject o = it2.next();
                    Boolean isResourceKey = (Boolean) o.get("isNameResourceKey");
                    p.addOption(new Option(
                            isResourceKey ? I18N.getResource((String) o.get("name")) : (String) o.get("name"),
                            (String) o.get("value"), (Boolean) o.get("selected")));
                }
                prompts.add(p);
                break;
            }
            default: {
                prompts.add(new Prompt(type, (String) field.get("resourceKey"),
                        (String) field.get("defaultValue")));
                break;
            }
            }

        }

        return lastResultSuccessfull == null ? true : lastResultSuccessfull;
    } else {
        JSONObject session = (JSONObject) result.get("session");
        sessionId = (String) session.get("id");
        principalName = (String) ((JSONObject) session.get("currentPrincipal")).get("principalName");
        return true;
    }
}