List of usage examples for org.json.simple JSONValue parse
public static Object parse(String s)
From source file:com.walmartlabs.mupd8.application.Config.java
private JSONObject applyFiles(JSONObject destination, File... configFiles) throws IOException { for (File file : configFiles) { Reader reader = new FileReader(file); // TODO Switch to try (Reader reader = new FileReader(file)) { ... } for Java 7. try {/*from w ww . ja v a 2 s . c om*/ JSONObject parsedFile = (JSONObject) JSONValue.parse(readWithPreprocessing(reader)); apply(destination, parsedFile); } finally { if (reader != null) { reader.close(); } } } return destination; }
From source file:com.rmtheis.yandtran.YandexTranslatorAPI.java
private static String[] jsonObjValToStringArr(final String inputString, final String subObjPropertyName) throws Exception { JSONObject jsonObj = (JSONObject) JSONValue.parse(inputString); JSONArray jsonArr = (JSONArray) jsonObj.get(subObjPropertyName); return jsonArrToStringArr(jsonArr.toJSONString(), null); }
From source file:myproject.MyServer.java
public static void Delete(HttpServletRequest request, HttpServletResponse response) throws IOException { try {/*from w w w .ja v a 2 s. c om*/ String jsonString = IOUtils.toString(request.getInputStream()); JSONObject json = (JSONObject) JSONValue.parse(jsonString); Long student_id = (Long) json.get("student_id"); String query = String.format("DELETE FROM student " + "WHERE student_id=%d", student_id); database.runUpdate(query); JSONObject output = new JSONObject(); output.put("result", true); response.getWriter().write(JSONValue.toJSONString(output)); } catch (Exception ex) { JSONObject output = new JSONObject(); output.put("error", "Connection failed: " + ex.getMessage()); response.getWriter().write(JSONValue.toJSONString(output)); } }
From source file:eu.hansolo.fx.weatherfx.GeoCode.java
/** * Returns a JavaFX Point2D object that contains latitude (y) and longitude(x) of the given * Address.//w w w . j a v a2 s .com * Example format for STREET_CITY_COUNTRY: "1060 W. Addison St., Chicago IL, 60613" * @param STREET_CITY_COUNTRY * @return a JavaFX Point2D object that contains latitude(y) and longitude(x) of given address */ public static Point2D geoCode(final String STREET_CITY_COUNTRY) throws UnsupportedEncodingException { String URL_STRING = new StringBuilder(GEO_CODE_URL).append(URLEncoder.encode(STREET_CITY_COUNTRY, "UTF-8")) .append("&thumbMaps=false").toString(); StringBuilder response = new StringBuilder(); try { final HttpsURLConnection CONNECTION = (HttpsURLConnection) new URL(URL_STRING).openConnection(); final BufferedReader IN = new BufferedReader(new InputStreamReader(CONNECTION.getInputStream())); String inputLine; while ((inputLine = IN.readLine()) != null) { response.append(inputLine).append("\n"); } IN.close(); Object obj = JSONValue.parse(response.toString()); JSONObject jsonObj = (JSONObject) obj; JSONArray results = (JSONArray) jsonObj.get("results"); JSONObject firstResult = (JSONObject) results.get(0); JSONArray locations = (JSONArray) firstResult.get("locations"); JSONObject firstLocation = (JSONObject) locations.get(0); JSONObject latLng = (JSONObject) firstLocation.get("latLng"); return new Point2D(Double.parseDouble(latLng.get("lng").toString()), Double.parseDouble(latLng.get("lat").toString())); } catch (IOException ex) { System.out.println(ex); return new Point2D(0, 0); } }
From source file:me.neatmonster.spacebukkit.PanelListener.java
/** * Interprets a raw command from the panel (multiple) * @param string input from panel//from ww w .j a va 2s. c o m * @return result of the action * @throws InvalidArgumentsException Thrown when the wrong arguments are used by the panel * @throws UnhandledActionException Thrown when there is no handler for the action */ @SuppressWarnings("unchecked") private static Object interpretm(final String string) throws InvalidArgumentsException, UnhandledActionException { final int indexOfMethod = string.indexOf("?method="); final int indexOfArguments = string.indexOf("&args="); final int indexOfKey = string.indexOf("&key="); final String methodString = string.substring(indexOfMethod + 8, indexOfArguments); final String argumentsString = string.substring(indexOfArguments + 6, indexOfKey); final List<Object> methods = (List<Object>) JSONValue.parse(methodString); final List<Object> arguments = (List<Object>) JSONValue.parse(argumentsString); final List<Object> result = (List<Object>) JSONValue.parse("[]"); for (int i = 0; i < methods.size(); i++) { String argsString = arguments.toArray()[i].toString(); List<Object> args = (List<Object>) JSONValue.parse(argsString); try { if (SpaceBukkit.getInstance().actionsManager.contains(methods.toArray()[i].toString())) result.add(SpaceBukkit.getInstance().actionsManager.execute(methods.toArray()[i].toString(), args.toArray())); else { final RequestEvent event = new RequestEvent(methods.toArray()[i].toString(), args.toArray()); Bukkit.getPluginManager().callEvent(event); result.add(JSONValue.toJSONString(event.getResult())); } } catch (final InvalidArgumentsException e) { result.add(null); e.printStackTrace(); } catch (final UnhandledActionException e) { result.add(null); e.printStackTrace(); } } return result; }
From source file:control.ProcesoVertimientosServlets.InsertarProgramacionMonitoreo.java
/** * Handles the HTTP <code>POST</code> method. * * @param request servlet request/* w w w .jav 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 */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { JSONObject respError = new JSONObject(); try { //Obtenemos el numero de contrato String consultorMonitoreo = request.getParameter("consultorMonitoreo"); String fechaMonitoreo = request.getParameter("fechaMonitoreo"); String horaInicioMonitoreo = request.getParameter("horaInicioMonitoreo"); String horaFinMonitoreo = request.getParameter("horaFinMonitoreo"); int laboratorioMonitoreo = Integer.parseInt(request.getParameter("laboratorioMonitoreo")); int codigoProceso = Integer.parseInt(request.getParameter("codigoProceso")); String observacion = request.getParameter("observacionesReprogramacion"); String duracionMonitoreo = request.getParameter("duracionMonitoreo"); //Insertamos el programacion del monitoreo y obtenemos el codigo ProgramarMonitoreo manager = new ProgramarMonitoreo(); int codigoMonitoreo = manager.insertar(consultorMonitoreo, fechaMonitoreo, horaInicioMonitoreo, horaFinMonitoreo, laboratorioMonitoreo, codigoProceso, observacion, duracionMonitoreo); //Obtenemos la cadena con la informacion y la convertimos en un //JSONArray String puntos = request.getParameter("puntosVertimiento"); 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++) { //Obtenemos la info de los puntos JSONObject jsonObject = (JSONObject) jsonArray.get(i); int codigoPunto = Integer.parseInt((String) jsonObject.get("codigo")); int codigoActividad = Integer.parseInt((String) jsonObject.get("actividad")); //Creamos el manager y guardamos la informacion. manager.insertarPuntoMonitoreo(codigoPunto, codigoActividad, codigoMonitoreo); } respError.put("error", "1"); response.getWriter().write(respError.toString()); } catch (Exception ex) { respError.put("error", "0"); response.getWriter().write(respError.toString()); } }
From source file:de.matzefratze123.heavyspleef.util.Updater.java
public void query() { URL url;/*from w ww.j ava 2 s . c o m*/ try { url = new URL(API_HOST + API_QUERY + PROJECT_ID); } catch (MalformedURLException e) { e.printStackTrace(); return; } try { URLConnection conn = url.openConnection(); conn.setConnectTimeout(6000); conn.addRequestProperty("User-Agent", "HeavySpleef-Updater (by matzefratze123)"); final BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); String query = reader.readLine(); JSONArray array = (JSONArray) JSONValue.parse(query); String version = null; if (array.size() > 0) { // Get the newest file's details JSONObject latest = (JSONObject) array.get(array.size() - 1); fileTitle = (String) latest.get(API_TITLE_VALUE); fileName = (String) latest.get(API_NAME_VALUE); downloadUrl = (String) latest.get(API_LINK_VALUE); String[] parts = fileTitle.split(" v"); if (parts.length >= 2) { version = parts[1]; } checkVersions(version); done = true; } } catch (IOException e) { Logger.severe("Failed querying the curseforge api: " + e.getMessage()); e.printStackTrace(); } }
From source file:com.p000ison.dev.simpleclans2.util.JSONUtil.java
public static Set<Long> JSONToLongSet(String json) { if (json == null || json.isEmpty()) { return null; }/*from w ww . j a va2 s . c o m*/ JSONArray parser = (JSONArray) JSONValue.parse(json); if (parser == null) { return null; } Set<Long> set = new HashSet<Long>(); for (Object obj : parser) { set.add((Long) obj); } return set; }
From source file:com.cnaude.purpleirc.Utilities.UpdateChecker.java
private String updateCheck(String mode) { String message;// w ww .j a va 2 s . c o m try { URL url = new URL("http://h.cnaude.org:8081/job/PurpleIRC-forge/lastStableBuild/api/json"); URLConnection conn = url.openConnection(); conn.setReadTimeout(5000); conn.addRequestProperty("User-Agent", "PurpleIRC-forge Update Checker"); conn.setDoOutput(true); final BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); final String response = reader.readLine(); final JSONObject obj = (JSONObject) JSONValue.parse(response); if (obj.isEmpty()) { return plugin.LOG_HEADER_F + " No files found, or Feed URL is bad."; } newVersion = obj.get("number").toString(); String downloadUrl = obj.get("url").toString(); plugin.logDebug("newVersionTitle: " + newVersion); newBuild = Integer.valueOf(newVersion); if (newBuild > currentBuild) { message = plugin.LOG_HEADER_F + " Latest dev build: " + newVersion + " is out!" + " You are still running build: " + currentVersion; message = message + plugin.LOG_HEADER_F + " Update at: " + downloadUrl; } else if (currentBuild > newBuild) { message = plugin.LOG_HEADER_F + " Dev build: " + newVersion + " | Current build: " + currentVersion; } else { message = plugin.LOG_HEADER_F + " No new version available"; } } catch (IOException | NumberFormatException e) { message = plugin.LOG_HEADER_F + " Error checking for latest dev build: " + e.getMessage(); } return message; }
From source file:com.webkruscht.wmt.WebmasterTools.java
/** * Get a list of available search data downloads for a particular site * @param site//from w w w.j a va2s . c o m * @return HashMap of available download paths * @throws Exception */ public JSONObject getDownloadList(SitesEntry site) throws Exception { JSONObject data = null; URL url = _getUrl(String.format(dl_list_url, lang, site.getTitle().getPlainText())); GDataRequest req = svc.createRequest(RequestType.QUERY, url, ContentType.JSON); try { req.execute(); data = (JSONObject) JSONValue.parse(new InputStreamReader(req.getResponseStream())); } catch (RedirectRequiredException e) { return null; } return data; }