List of usage examples for org.json.simple JSONArray iterator
public Iterator<E> iterator()
From source file:edu.pdx.konstan2.PortlandLive.responseParserFactory.java
public void parseArrivalsXML(String response, HashMap<String, Arrival> sMap) { try {/*from w ww. ja va2s . c o m*/ JSONParser parser = new JSONParser(); JSONObject jobj = (JSONObject) parser.parse(response.toString()); JSONObject v = (JSONObject) jobj.get("resultSet"); JSONArray arr = (JSONArray) v.get("arrival"); Iterator<JSONObject> iter = arr.iterator(); while (iter.hasNext()) { Arrival t = new Arrival(iter.next()); sMap.put(t.id, t); } } catch (Exception e) { Log.d("exception", e.toString()); } }
From source file:com.nmi.uk.librarycl.JsnParser.java
/** * Method to construct the json object and json array form the source file. * The extracted elements are stored to `LibraryRecord.java` objects.<br> * Duplicate `LibraryRecord` removed before adding to the array using hash of content. *///from w w w. j a va 2 s.c om public void parseJson() { try { JSONParser parser = new JSONParser(); jsonReader = new BufferedReader( new InputStreamReader(getClass().getResourceAsStream("/resources/bangor-library.json"))); JSONObject jsonObj = (JSONObject) parser.parse(jsonReader); String name = (String) jsonObj.get("name"); libraryName = name; JSONArray extractedBooks = (JSONArray) jsonObj.get("books"); Iterator i = extractedBooks.iterator(); while (i.hasNext()) { rec = new LibraryRecord(); rec.setLib_name(libraryName); JSONObject innerObj = (JSONObject) i.next(); rec.setBook_name(innerObj.get("name").toString()); rec.setAuth_name(innerObj.get("author").toString()); rec.setCat_name(innerObj.get("category").toString()); if (!LibraryAccess.bookShelves.isEmpty()) { for (LibraryRecord bookSaved : LibraryAccess.bookShelves) { if (this.rec.getHashOfContent() == bookSaved.getHashOfContent()) { duplicate = true; rec = null; } } if (!duplicate) { LibraryAccess.addRecord(rec); } duplicate = false; } else { System.out.println("Library empty : Adding records..."); LibraryAccess.addRecord(this.rec); } } } catch (FileNotFoundException ex) { Logger.getLogger(JsnParser.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(JsnParser.class.getName()).log(Level.SEVERE, null, ex); } catch (ParseException ex) { Logger.getLogger(JsnParser.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:edu.pdx.konstan2.PortlandLive.responseParserFactory.java
public void parseArrivals(String response, HashMap<String, Arrival> sMap) { try {//from www .ja v a2 s .c om JSONParser parser = new JSONParser(); JSONObject jobj = (JSONObject) parser.parse(response.toString()); JSONObject v = (JSONObject) jobj.get("resultSet"); JSONArray arr = (JSONArray) v.get("arrival"); Iterator<JSONObject> iter = arr.iterator(); while (iter.hasNext()) { Arrival t = new Arrival(iter.next()); sMap.put(t.id, t); iter.remove(); } } catch (Exception e) { Log.d("exception", e.toString()); } }
From source file:me.uni.sushilkumar.geodine.util.YoutubeSearch.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods. * @param request servlet request//from w w w . j a v a 2s .c o m * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ParseException, SQLException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { String type = request.getParameter("type"); String query = null; type += ""; if (type.equals("random")) { DBConnection con = new DBConnection(); ArrayList<String> cuisineList = con.getCuisineList(); Random generator = new Random(); query = cuisineList.get(generator.nextInt(cuisineList.size())); } else { query = request.getParameter("q"); } query += " recipe"; URL u = new URL("http://gdata.youtube.com/feeds/api/videos?q=" + URLEncoder.encode(query, "UTF-8") + "&max-results=1&v=2&alt=jsonc"); BufferedReader br = new BufferedReader(new InputStreamReader(u.openStream())); JSONParser parser = new JSONParser(); Object obj = parser.parse(br); JSONObject json = (JSONObject) obj; JSONObject data = (JSONObject) json.get("data"); JSONArray items = (JSONArray) data.get("items"); Iterator<JSONObject> it = items.iterator(); if (it != null) { JSONObject item = it.next(); String id = (String) item.get("id"); String title = (String) item.get("title"); out.println(id); } else { out.println("Unable to fetch any video"); } } finally { out.close(); } }
From source file:com.kodyhusky.kodymc.UUIDFetcher.java
@Override @SuppressWarnings("SleepWhileInLoop") public Map<String, UUID> call() throws Exception { Map<String, UUID> uuidMap = new HashMap<>(); int requests = (int) Math.ceil(names.size() / PROFILES_PER_REQUEST); for (int i = 0; i < requests; i++) { HttpURLConnection connection = createConnection(); String body = JSONArray.toJSONString(names.subList(i * 100, Math.min((i + 1) * 100, names.size()))); writeBody(connection, body);// w w w . j av a 2 s .c om JSONArray array = (JSONArray) jsonParser.parse(new InputStreamReader(connection.getInputStream())); for (Iterator it = array.iterator(); it.hasNext();) { Object profile = it.next(); JSONObject jsonProfile = (JSONObject) profile; String id = (String) jsonProfile.get("id"); String name = (String) jsonProfile.get("name"); UUID uuid = UUIDFetcher.getUUID(id); uuidMap.put(name, uuid); } if (rateLimiting && i != requests - 1) { Thread.sleep(100L); } } return uuidMap; }
From source file:net.phyloviz.goeburst.tree.GOeBurstMSTItemFactory.java
private Map<Integer, GOeBurstNode> getNodes(Map<String, GOeBurstNode> td, JSONArray onodes) { Map<Integer, GOeBurstNode> nodes = new HashMap<>(); for (Iterator<JSONObject> nIt = onodes.iterator(); nIt.hasNext();) { JSONObject node = nIt.next();/*from www. j ava2 s.c o m*/ Integer uid = (int) (long) node.get("id"); String profile = (String) node.get("profile"); JSONArray group_lvs = (JSONArray) node.get("group-lvs"); GOeBurstNode n = td.get(profile); n.lv = new int[group_lvs.size()]; for (int i = 0; i < group_lvs.size(); i++) n.lv[i] = (int) (long) group_lvs.get(i); nodes.put(uid, n); } return nodes; }
From source file:de.ingrid.iplug.ckan.index.producer.CkanRecordSetProducer.java
@SuppressWarnings("unchecked") private void createRecordIdsFromSearch() { try {// ww w . java 2s. co m if (log.isDebugEnabled()) { log.debug("Requesting URL: " + searchUrl); } JSONObject parse = requestJsonUrl(searchUrl); Long count = (Long) parse.get("count"); JSONArray results = (JSONArray) parse.get("results"); this.recordIdIterator = results.iterator(); this.numRecords = count.intValue(); } catch (Exception e) { log.error("Error creating record ids.", e); } }
From source file:edu.pdx.konstan2.PortlandLive.StopsFactory.java
public void parse(JSONObject v) { locID = (Long) v.get("locid"); direction = (String) v.get("dir"); longitude = (Double) v.get("lng"); latitude = (Double) v.get("lat"); description = (String) v.get("desc"); JSONArray arr = (JSONArray) v.get("routes"); Iterator<JSONObject> iter = arr.iterator(); while (iter.hasNext()) { Route r = new Route(iter.next()); routes.add(r);// www . j a va2 s .c o m } }
From source file:au.com.optus.pei.express.coef2.template.businessreq.BusinessRequestTemplate.java
public String brTemplate(String s) { StringBuilder sb = new StringBuilder(); JSONParser parser = new JSONParser(); try {/* ww w . ja v a 2 s . c o m*/ Object obj = parser.parse(s); JSONArray array = (JSONArray) obj; Map<String, String> map = new HashMap<String, String>(); Iterator<JSONObject> iterator = array.iterator(); while (iterator.hasNext()) { JSONObject factObj = (JSONObject) iterator.next(); map.put((String) factObj.get("name"), (String) factObj.get("value")); } sb.append("Dear EIES Engagement,"); sb.append( "\nA new engagement request has been submitted via the online COEF Business Engagement Form. Details are as follows:"); sb.append("\n\nRequest ID: 107"); sb.append("\nName: "); sb.append(map.get("firstName")); sb.append("\nEmail:"); sb.append(map.get("email")); sb.append("\nPhone: "); sb.append(map.get("phone")); sb.append("\nMobile:"); sb.append(map.get("od[employeeMobile]")); sb.append("\nEmployee ID: "); sb.append(map.get("employeeID")); sb.append("\nWhat are you trying to achieve? "); sb.append(map.get("requestDescription")); if (map.containsKey("proposedStartDate")) { sb.append("\nWhen are you ready to start? "); sb.append(map.get("proposedStartDate")); } if (map.containsKey("proposedAvailableDate")) { sb.append("\nWhen would you like this change to be available? "); sb.append(map.get("proposedAvailableDate")); } sb.append("\nType of work requested: "); sb.append(map.get("requestType")); sb.append("\nCFU or Business Unit: "); sb.append(map.get("cfu")); sb.append("\nHas funding been identified for your request? "); sb.append(map.get("fundIdentified")); if (map.get("fundIdentified").equals("Yes")) { sb.append("\nWho will fund this? "); sb.append(map.get("sponsername")); } else { sb.append("\nDo you have a sponsor for this request? "); sb.append(map.get("haveSponser")); } } catch (ParseException pe) { // throws ; } return sb.toString(); }
From source file:net.phyloviz.goeburst.tree.GOeBurstMSTItemFactory.java
private Collection<Edge<GOeBurstNode>> createEdges(JSONArray edgesArray, Map<Integer, GOeBurstNode> nodes) { List<Edge<GOeBurstNode>> edges = new ArrayList<>(); for (Iterator<JSONObject> eIt = edgesArray.iterator(); eIt.hasNext();) { JSONObject edge = eIt.next();/*from w w w . ja va 2 s . co m*/ Integer u = (int) (long) edge.get("u"); Integer v = (int) (long) edge.get("v"); edges.add(new Edge(nodes.get(u), nodes.get(v))); } return edges; }