List of usage examples for org.json.simple JSONArray get
public E get(int index)
From source file:net.bashtech.geobot.JSONUtil.java
public static String xboxLastGameProgress(String gamertag) { try {/*from w w w . j ava2 s .c om*/ JSONParser parser = new JSONParser(); Object obj = parser.parse( BotManager.getRemoteContent("http://www.xboxleaders.com/api/games.json?gamertag=" + gamertag)); JSONObject jsonObject = (JSONObject) obj; JSONObject data = (JSONObject) jsonObject.get("data"); JSONArray games = (JSONArray) data.get("games"); JSONObject lastGame = (JSONObject) games.get(0); String percent = (String) lastGame.get("progress").toString(); return percent; } catch (Exception ex) { ex.printStackTrace(); return "(unavailable)"; } }
From source file:net.bashtech.geobot.JSONUtil.java
public static String xboxLastGame(String gamertag) { try {//from ww w . j a v a2 s .c o m JSONParser parser = new JSONParser(); Object obj = parser.parse( BotManager.getRemoteContent("http://www.xboxleaders.com/api/games.json?gamertag=" + gamertag)); JSONObject jsonObject = (JSONObject) obj; JSONObject data = (JSONObject) jsonObject.get("data"); JSONArray games = (JSONArray) data.get("games"); JSONObject lastGame = (JSONObject) games.get(0); String title = (String) lastGame.get("title"); return title; } catch (Exception ex) { ex.printStackTrace(); return "(unavailable)"; } }
From source file:edu.ncsu.epc.models.GetInfo.java
public ApartmentInfo parseJSON(String response, String addr) { ApartmentInfo aptInfo = null;/* w w w. ja va 2 s . co m*/ try { JSONObject json = (JSONObject) new JSONParser().parse(response); JSONArray jsonArray = (JSONArray) new JSONParser().parse(json.get("collection").toString()); for (int i = 0; i < jsonArray.size(); i++) { JSONObject json2 = (JSONObject) new JSONParser().parse(jsonArray.get(i).toString()); if (addr.startsWith(json2.get("address").toString())) { aptInfo = new ApartmentInfo(); aptInfo.addr = json2.get("address").toString(); if (json2.get("city") != null) aptInfo.city = json2.get("city").toString(); if (json2.get("state") != null) aptInfo.state = json2.get("state").toString(); if (json2.get("bedrooms") == null) { JSONArray jsonArray1 = (JSONArray) new JSONParser() .parse(json2.get("latest_prices").toString()); JSONObject jsonlatest_price = (JSONObject) new JSONParser() .parse(jsonArray1.get(0).toString()); aptInfo.noOfBedrooms = Double.parseDouble(jsonlatest_price.get("bedrooms").toString()); aptInfo.rent = Double.parseDouble(jsonlatest_price.get("rent").toString()); } else { aptInfo.noOfBedrooms = Double.parseDouble(json2.get("bedrooms").toString()); aptInfo.rent = Double.parseDouble(json2.get("rent").toString()); } if (json2.get("rent") != null) aptInfo.rent = Double.parseDouble(json2.get("rent").toString()); aptInfo.zip = (String) json2.get("zip_code"); if (json2.get("year_built") != null) aptInfo.year_built = Double.parseDouble(json2.get("year_built").toString()); } } } catch (Exception e) { e.printStackTrace(); } return aptInfo; }
From source file:control.ParametrizacionServlets.InsertarPuntoVertimientoContrato.java
/** * Handles the HTTP <code>POST</code> method. * * @param request servlet request/*from www .j a v a 2s .com*/ * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { //Obtenemos el numero de contrato Double contrato = Double.parseDouble(request.getParameter("contrato")); //Obtenemos la cadena con la informacion y la convertimos en un //JSONArray String puntos = request.getParameter("puntos"); Object obj = JSONValue.parse(puntos); JSONArray jsonArray = new JSONArray(); jsonArray = (JSONArray) obj; //Recorremos el JSONArray y obtenemos la informacion. for (int i = 0; i < jsonArray.size(); i++) { JSONObject jsonObject = (JSONObject) jsonArray.get(i); String ubicacion = (String) jsonObject.get("ubicacion"); String latitud = (String) jsonObject.get("latitud"); String longitud = (String) jsonObject.get("longitud"); String observacion = (String) jsonObject.get("observacion"); String tipoEstructura = (String) jsonObject.get("tipoEstructura"); int estado = Integer.parseInt((String) jsonObject.get("estado")); //Creamos el manager y guardamos la informacion. PuntosVertimiento manager = new PuntosVertimiento(); manager.insertar(ubicacion, latitud, longitud, observacion, estado, contrato, tipoEstructura); } } catch (Exception ex) { //Logger.getLogger(InsertarActEconomica.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:net.bashtech.geobot.JSONUtil.java
public static String defineUrban(String word) { try {/* w w w .j a v a 2 s .c o m*/ JSONParser parser = new JSONParser(); Object obj = parser .parse(BotManager.getRemoteContent("http://api.urbandictionary.com/v0/define?term=" + word)); JSONObject jsonObject = (JSONObject) obj; JSONArray items = (JSONArray) jsonObject.get("list"); JSONObject items0 = (JSONObject) items.get(0); String title = (String) items0.get("definition"); return title; } catch (Exception ex) { return "Couldn't find any results, sorry"; } }
From source file:com.piusvelte.webcaster.MediaLoader.java
@Override public List<Medium> loadInBackground() { List<Medium> media = new ArrayList<Medium>(); if (mediaUrl != null) { HttpURLConnection httpURLConnection; try {/* w ww. j av a2s . c om*/ String response; httpURLConnection = (HttpURLConnection) mediaUrl.openConnection(); InputStream in = new BufferedInputStream(httpURLConnection.getInputStream()); byte[] buffer = new byte[512]; ByteArrayOutputStream content = new ByteArrayOutputStream(); int readBytes = 0; while ((readBytes = in.read(buffer)) != -1) { content.write(buffer, 0, readBytes); } response = new String(content.toByteArray()); JSONParser jsonParser = new JSONParser(); JSONArray mediaJArr = (JSONArray) jsonParser.parse(response); Gson gson = new Gson(); for (int i = 0, s = mediaJArr.size(); i < s; i++) { media.add(gson.fromJson(mediaJArr.get(i).toString(), Medium.class)); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return media; }
From source file:net.bashtech.geobot.JSONUtil.java
public static String youtubeTitle(String id) { String api_key = BotManager.getInstance().YoutubeAPIKey; try {/*from w ww.j av a2s.c o m*/ JSONParser parser = new JSONParser(); Object obj = parser.parse(BotManager.getRemoteContent( "https://www.googleapis.com/youtube/v3/videos?id=" + id + "&key=" + api_key + "&part=snippet")); JSONObject jsonObject = (JSONObject) obj; JSONArray items = (JSONArray) jsonObject.get("items"); JSONObject items0 = (JSONObject) items.get(0); JSONObject snippet = (JSONObject) items0.get("snippet"); String title = (String) snippet.get("title"); return title; } catch (Exception ex) { ex.printStackTrace(); return null; } }
From source file:org.apache.tika.parser.captioning.tf.TensorflowRESTCaptioner.java
@Override public List<CaptionObject> recognise(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException { List<CaptionObject> capObjs = new ArrayList<>(); try {/* w w w .j a v a 2 s.c om*/ DefaultHttpClient client = new DefaultHttpClient(); HttpPost request = new HttpPost(getApiUri(metadata)); try (ByteArrayOutputStream byteStream = new ByteArrayOutputStream()) { //TODO: convert this to stream, this might cause OOM issue // InputStreamEntity is not working // request.setEntity(new InputStreamEntity(stream, -1)); IOUtils.copy(stream, byteStream); request.setEntity(new ByteArrayEntity(byteStream.toByteArray())); } HttpResponse response = client.execute(request); try (InputStream reply = response.getEntity().getContent()) { String replyMessage = IOUtils.toString(reply); if (response.getStatusLine().getStatusCode() == 200) { JSONObject jReply = (JSONObject) new JSONParser().parse(replyMessage); JSONArray jCaptions = (JSONArray) jReply.get("captions"); for (int i = 0; i < jCaptions.size(); i++) { JSONObject jCaption = (JSONObject) jCaptions.get(i); String sentence = (String) jCaption.get("sentence"); Double confidence = (Double) jCaption.get("confidence"); capObjs.add(new CaptionObject(sentence, LABEL_LANG, confidence)); } } else { LOG.warn("Status = {}", response.getStatusLine()); LOG.warn("Response = {}", replyMessage); } } } catch (Exception e) { LOG.warn(e.getMessage(), e); } return capObjs; }
From source file:JavaCloud.Cloud.java
public ArrayList<Token> tokenList() throws CoreException { JSONObject object = new JSONObject(); object.put("login", login); object.put("pw_hash", Utils.calcHash(password, seed)); JSONArray jsontokens = (JSONArray) Utils.request(address, "/user/token/get_list/", object); ArrayList<Token> tokens = new ArrayList<Token>(); for (int i = 0; i < jsontokens.size(); i++) { tokens.add(new Token(address, login, password, seed, (JSONObject) jsontokens.get(i))); }// w ww. j a v a 2 s .c o m return tokens; }
From source file:com.ebay.logstorm.server.platform.spark.SparkExecutionPlatform.java
@Override public synchronized void status(final PipelineExecutionEntity entity) throws Exception { String applicationId = entity.getProperties().getProperty("applicationId"); if (applicationId == null) { LOG.warn("get null applicationId, may be starting"); return;/*w ww. j av a2s .c o m*/ } entity.requireUpdate(true); int beginPort = MIN_REST_PORT; while (beginPort++ < MAX_REST_PORT) { String restURL = sparkRestUrl + ":" + beginPort + SPARK_REST_API_PATH + applicationId; try { URL url = new URL(restURL); BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream())); String statusStr = ""; String line; while (null != (line = br.readLine())) { statusStr += line + "\n"; } br.close(); entity.setDescription(statusStr); entity.setUrl(sparkRestUrl + ":" + beginPort + SPARK_JOB_PATH); LOG.info(statusStr); //parse more json fields later, just work now JSONParser parser = new JSONParser(); Object obj = parser.parse(statusStr); JSONObject jsonObject = (JSONObject) obj; if (applicationId.equals(jsonObject.get("id"))) { LOG.info("find application {} rest url {}", applicationId, restURL); } else { LOG.warn("wrong application {} rest url {}", applicationId, restURL); continue; } JSONArray a = (JSONArray) jsonObject.get("attempts"); for (int i = 0; i < a.size(); i++) { boolean finished = (boolean) ((JSONObject) a.get(i)).get("completed"); if (!finished) { entity.setStatus(PipelineExecutionStatus.RUNNING); return; } } entity.setStatus(PipelineExecutionStatus.STOPPED); return; } catch (Exception e) { } } entity.setStatus(PipelineExecutionStatus.STOPPED); LOG.warn("get status for application {} failed, assume stopped", applicationId); }