List of usage examples for org.json.simple JSONArray iterator
public Iterator<E> iterator()
From source file:me.uni.sushilkumar.geodine.util.YummlyImage.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 a2s. 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 { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { String id = request.getParameter("id"); JSONParser parser = new JSONParser(); URL u = new URL("http://api.yummly.com/v1/api/recipe/" + id + "?_app_id=bad04ef2&_app_key=f2df74726bb514d9390abf1e6c9ad4f0"); Object json = parser.parse(new BufferedReader(new InputStreamReader(u.openStream()))); JSONObject obj = (JSONObject) json; JSONArray images = (JSONArray) obj.get("images"); Iterator<JSONObject> it = images.iterator(); JSONObject temp = it.next(); String url = (String) temp.get("hostedLargeUrl"); out.println("<h1>" + url + "</h1>"); } catch (ParseException ex) { Logger.getLogger(YummlyImage.class.getName()).log(Level.SEVERE, null, ex); } finally { out.close(); } }
From source file:com.punyal.blackhole.core.net.lwm2m.LWM2Msniffer.java
private void checkConnectedMulles(String response) { JSONArray jsonArray = (JSONArray) JSONValue.parse(response); Iterator i = jsonArray.iterator(); LWM2Mdevice device;/*from w w w . j a va2 s . c om*/ while (i.hasNext()) { JSONObject json = (JSONObject) i.next(); String name = json.get("endPoint").toString(); String id = json.get("registrationId").toString(); String host = json.get("address").toString().substring(1); int port = Integer.parseInt(json.get("port").toString()); //System.out.println(name+" ("+id+") "+host+":"+port); device = devicesList.getDeviceByName(name); if (device == null) // Add new device devicesList.addDevice(name, id, host, port); else { if (!device.getId().equals(id)) { //System.out.println(name+" new id:"+id+ " old id:"+device.getId()); //System.out.println("Last update:"+ device.getLastUpdate()); device.updateID(id); //System.out.println("Current time:"+ device.getLastUpdate()); } } } }
From source file:cc_package.CC_Conf.java
public CC_Conf(String file) { JSONParser parser = new JSONParser(); units = new ArrayList<>(); names = new ArrayList<>(); try {//from w w w . j av a 2s.c om JSONObject jsonObject; InputStream is = this.getClass().getResourceAsStream("/" + file); jsonObject = (JSONObject) parser.parse(getStringFromInputStream(is)); //jsonObject = (JSONObject) parser.parse(is); //Object obj = parser.parse(new FileReader("cc_test.json")); //JSONObject jsonObject = (JSONObject) obj; JSONArray junits = (JSONArray) jsonObject.get("units"); Iterator<JSONObject> iterator = junits.iterator(); while (iterator.hasNext()) { JSONObject all = iterator.next(); String country = (String) all.get("CountryCurrency"); String unit = (String) all.get("Units"); units.add(unit); names.add(country); } } catch (Exception e) { e.printStackTrace(); } }
From source file:buspathcontroller.JSONFileParser.java
public void generateRouteStopsAndPath() { JSONParser parser = new JSONParser(); ArrayList<String> routeList = new ArrayList<String>(); try {// w w w. jav a2s . c o m FileReader reader = new FileReader("/Users/Zhaowei/Desktop/BusPath/allRoutes.txt"); BufferedReader br = new BufferedReader(reader); String line; while ((line = br.readLine()) != null) { String routeName = line.split(";")[1]; routeList.add(routeName); } br.close(); reader.close(); Iterator<String> it = routeList.iterator(); while (it.hasNext()) { String routeName = it.next(); PrintWriter writer = new PrintWriter( "/Users/Zhaowei/Desktop/BusPath/routeInfo/stops/" + routeName + ".txt"); Object obj = parser.parse( new FileReader("/Users/Zhaowei/Desktop/BusPath/RawJSON/route/" + routeName + ".json")); JSONObject jsonObject = (JSONObject) obj; JSONObject route = (JSONObject) jsonObject.get("route"); JSONArray directions = (JSONArray) route.get("directions"); for (int i = 0; i < directions.size(); i++) { JSONObject direction = (JSONObject) directions.get(i); writer.println(direction.get("direction")); JSONArray stops = (JSONArray) direction.get("stops"); Iterator iter = stops.iterator(); while (iter.hasNext()) { JSONObject stop = (JSONObject) iter.next(); writer.println(stop.get("stopnumber") + ";" + stop.get("stoptitle") + ";" + stop.get("stoplat") + ";" + stop.get("stoplng")); } } writer.close(); } } catch (Exception e) { System.out.println(e); } }
From source file:currencyconverter.CurrencyConfiguration.java
/** * @param args the command line arguments *///from w w w . j a va2s.c o m public CurrencyConfiguration(String file) { JSONParser parser = new JSONParser(); units = new ArrayList<>(); names = new ArrayList<>(); try { JSONObject jsonObject; InputStream is = this.getClass().getResourceAsStream("/" + file); jsonObject = (JSONObject) parser.parse(getStringFromInputStream(is)); //jsonObject = (JSONObject) parser.parse(is); //Object obj = parser.parse(new FileReader("cc_test.json")); //JSONObject jsonObject = (JSONObject) obj; JSONArray junits = (JSONArray) jsonObject.get("units"); Iterator<JSONObject> iterator = junits.iterator(); while (iterator.hasNext()) { JSONObject all = iterator.next(); String country = (String) all.get("CountryCurrency"); String unit = (String) all.get("Units"); units.add(unit); names.add(country); } } catch (Exception e) { e.printStackTrace(); } }
From source file:Basics.JsonCityReader.java
private void jsonBreakDown(String foo, int i) { try {/*from w w w. j av a2s . c om*/ String jString = IOUtils.toString(new URL(foo)); JSONObject jObject = (JSONObject) JSONValue.parseWithException(jString); JSONArray stations = (JSONArray) jObject.get("stations"); Iterator it = stations.iterator(); jObject = (JSONObject) it.next(); //Getting city information. String tempID = (String) jObject.get("id"); String tempScore = (String) jObject.get("score"); String tempDistance = (String) jObject.get("distance"); String Coordinates = jObject.get("coordinate").toString(); //Getting Coordinates. JSONObject coordinate = (JSONObject) JSONValue.parseWithException(Coordinates); String tempType = (String) coordinate.get("type"); Double tempX = (Double) coordinate.get("x"); Double tempY = (Double) coordinate.get("y"); //Creating temporary city with the information. Coordinates tempCoordinates = new Coordinates(tempType, tempX, tempY); Cities tempCity = new Cities(tempID, cities.get(i), tempScore, tempCoordinates, tempDistance); //Adding city to the list. tempCity.addCity(tempCity); //Catching errors and retrying. } catch (IOException | ParseException e) { if (counter == 0) { flag = false; System.out.println( "An error occurred while trying to get " + cities.get(i) + "'s information. \nRetrying..."); } if (counter < 5) { counter++; jsonBreakDown(foo, i); //If retrying fails more than four times, the application stops. } else { System.out.println("There was an issue with retrieving " + cities.get(i) + "'s data.\nPlease restart the application and try again."); System.exit(1); } } if (!flag) { System.out.println("The error has been resolved. Continuing to the next city."); counter = 0; flag = true; } }
From source file:com.rackspacecloud.blueflood.outputs.serializers.BatchedMetricsJSONOutputSerializerTest.java
@Test public void testBatchedEnumMetricsSerialization() throws Exception { final BatchedMetricsJSONOutputSerializer serializer = new BatchedMetricsJSONOutputSerializer(); final Map<Locator, MetricData> metrics = new HashMap<Locator, MetricData>(); for (int i = 0; i < 2; i++) { final MetricData metricData = new MetricData(FakeMetricDataGenerator.generateFakeEnumRollupPoints(), "unknown", MetricData.Type.ENUM); metrics.put(Locator.createLocatorFromPathComponents(tenantId, String.valueOf(i)), metricData); }//w w w . ja v a 2 s.c o m JSONObject jsonMetrics = serializer.transformRollupData(metrics, PlotRequestParser.DEFAULT_STATS); Assert.assertTrue(jsonMetrics.get("metrics") != null); JSONArray jsonMetricsArray = (JSONArray) jsonMetrics.get("metrics"); Iterator<JSONObject> metricsObjects = jsonMetricsArray.iterator(); Assert.assertTrue(metricsObjects.hasNext()); while (metricsObjects.hasNext()) { JSONObject singleMetricObject = metricsObjects.next(); Assert.assertTrue(singleMetricObject.get("unit").equals("unknown")); Assert.assertTrue(singleMetricObject.get("type").equals("enum")); JSONArray data = (JSONArray) singleMetricObject.get("data"); Assert.assertTrue(data != null); } }
From source file:com.rackspacecloud.blueflood.outputs.serializers.BatchedMetricsJSONOutputSerializerTest.java
@Test public void testBatchedMetricsSerialization() throws Exception { final BatchedMetricsJSONOutputSerializer serializer = new BatchedMetricsJSONOutputSerializer(); final Map<Locator, MetricData> metrics = new HashMap<Locator, MetricData>(); for (int i = 0; i < 2; i++) { final MetricData metricData = new MetricData(FakeMetricDataGenerator.generateFakeRollupPoints(), "unknown", MetricData.Type.NUMBER); metrics.put(Locator.createLocatorFromPathComponents(tenantId, String.valueOf(i)), metricData); }//from w ww . j a v a 2 s . c om JSONObject jsonMetrics = serializer.transformRollupData(metrics, filterStats); Assert.assertTrue(jsonMetrics.get("metrics") != null); JSONArray jsonMetricsArray = (JSONArray) jsonMetrics.get("metrics"); Iterator<JSONObject> metricsObjects = jsonMetricsArray.iterator(); Assert.assertTrue(metricsObjects.hasNext()); while (metricsObjects.hasNext()) { JSONObject singleMetricObject = metricsObjects.next(); Assert.assertTrue(singleMetricObject.get("unit").equals("unknown")); Assert.assertTrue(singleMetricObject.get("type").equals("number")); JSONArray data = (JSONArray) singleMetricObject.get("data"); Assert.assertTrue(data != null); } }
From source file:edu.pdx.konstan2.PortlandLive.responseParserFactory.java
public void parseVehiclesLocationJSON(String response, HashMap<LatLng, Vehicle> vMap) { try {//from w w w. j a v a2 s.co m JSONParser parser = new JSONParser(); JSONObject jobj = (JSONObject) parser.parse(response.toString()); JSONObject v = (JSONObject) jobj.get("resultSet"); JSONArray arr = (JSONArray) v.get("vehicle"); Iterator<JSONObject> iter = arr.iterator(); while (iter.hasNext()) { Vehicle t = new Vehicle(iter.next()); vMap.put(new LatLng(t.latitude, t.longitude), t); } } catch (Exception e) { } }
From source file:edu.pdx.konstan2.PortlandLive.responseParserFactory.java
public void parseStopsJSON(String response, HashMap<LatLng, Stop> sMap) { try {/*from w w w . j a v a 2 s.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("location"); Iterator<JSONObject> iter = arr.iterator(); while (iter.hasNext()) { Stop t = new Stop(iter.next()); sMap.put(new LatLng(t.latitude, t.longitude), t); } } catch (Exception e) { } }