List of usage examples for org.json.simple JSONValue parse
public static Object parse(String s)
From source file:com.appzone.sim.services.handlers.ReceiveSmsCheckServiceHandlerTest.java
public void testServeNormal() { final HttpServletRequest request = context.mock(HttpServletRequest.class); SmsRepository repository = new MemorySmsRepository(); ServiceHandler handler = new ReceiveSmsCheckServiceHandler(repository); // setting some dummy messages repository.add(new Sms("message", "001", 10)); repository.add(new Sms("message", "001", 12)); repository.add(new Sms("message", "001", 13)); repository.add(new Sms("message", "002", 10)); repository.add(new Sms("message", "002", 10)); context.checking(new Expectations() { {/*from w w w . j av a2s . co m*/ allowing(request).getParameter(DefaultKewordMatcher.SERVICE_KEYWORD); will(returnValue(ReceiveSmsCheckServiceHandler.MATCHING_KEYWORD)); allowing(request).getParameter(ReceiveSmsCheckServiceHandler.KEY_ADDRESS); will(returnValue("001")); allowing(request).getParameter(ReceiveSmsCheckServiceHandler.KEY_SINCE); will(returnValue("11")); } }); String jsonStr = handler.serve(request); JSONArray result = (JSONArray) JSONValue.parse(jsonStr); assertEquals(2, result.size()); }
From source file:com.portfolio.data.utils.PostForm.java
public static boolean updateResource(String sessionid, String backend, String uuid, String lang, String json) throws Exception { /// Parse and create xml from JSON JSONObject files = (JSONObject) JSONValue.parse(json); JSONArray array = (JSONArray) files.get("files"); if ("".equals(lang) || lang == null) lang = "fr"; JSONObject obj = (JSONObject) array.get(0); String ressource = ""; String attLang = " lang=\"" + lang + "\""; ressource += "<asmResource>" + "<filename" + attLang + ">" + obj.get("name") + "</filename>" + // filename "<size" + attLang + ">" + obj.get("size") + "</size>" + "<type" + attLang + ">" + obj.get("type") + "</type>" + // obj.get("url"); // Backend source, when there is multiple backend "<fileid" + attLang + ">" + obj.get("fileid") + "</fileid>" + "</asmResource>"; /// Send data to resource /// Server + "/resources/resource/file/" + uuid +"?lang="+ lang CloseableHttpClient httpclient = HttpClients.createDefault(); try {/*from w w w. j ava 2 s . c o m*/ HttpPut put = new HttpPut("http://" + backend + "/rest/api/resources/resource/" + uuid); put.setHeader("Cookie", "JSESSIONID=" + sessionid); // So that the receiving servlet allow us StringEntity se = new StringEntity(ressource); se.setContentEncoding("application/xml"); put.setEntity(se); CloseableHttpResponse response = httpclient.execute(put); try { HttpEntity resEntity = response.getEntity(); } finally { response.close(); } } finally { httpclient.close(); } return false; }
From source file:me.neatmonster.spacertk.plugins.PluginsManager.java
/** * Adds a plugin//w w w . jav a 2s . c om * @param pluginName Plugin to add * @return The created plugin object */ private SBPlugin addPlugin(final String pluginName) { try { final URLConnection connection = new URL("http://api.bukget.org/api2/bukkit/plugin/" + pluginName) .openConnection(); final BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(connection.getInputStream())); final StringBuffer stringBuffer = new StringBuffer(); String line; while ((line = bufferedReader.readLine()) != null) stringBuffer.append(line); bufferedReader.close(); final String result = stringBuffer.toString(); if (result == null || result.equals("")) return null; return new SBPlugin((JSONObject) JSONValue.parse(result)); } catch (final Exception e) { e.printStackTrace(); } return null; }
From source file:com.p000ison.dev.simpleclans2.util.JSONUtil.java
public static List<Long> JSONToLongList(String json) { if (json == null || json.isEmpty()) { return null; }//w w w. j a va2 s . c o m JSONArray parser = (JSONArray) JSONValue.parse(json); if (parser == null) { return null; } List<Long> list = new ArrayList<Long>(); for (Object obj : parser) { list.add((Long) obj); } return list; }
From source file:com.yahoo.storm.yarn.StormMasterServerHandler.java
@SuppressWarnings("unchecked") @Override// ww w .ja v a2 s.co m public void setStormConf(String storm_conf) throws TException { LOG.info("setting configuration..."); // stop processes stopSupervisors(); stopUI(); stopNimbus(); Object json = JSONValue.parse(storm_conf); Map<?, ?> new_conf = (Map<?, ?>) json; _storm_conf.putAll(new_conf); Util.rmNulls(_storm_conf); setStormHostConf(); // start processes startNimbus(); startUI(); startSupervisors(); }
From source file:com.appzone.sim.services.handlers.MtLogCheckServiceHandlerTest.java
public void testServeNormal() { final HttpServletRequest request = context.mock(HttpServletRequest.class); MtMessageRepository repository = new MemoryMtMessageRepository(); ServiceHandler handler = new MtLogCheckServiceHandler(repository); // setting some dummy messages repository.add(new MtMessage("message", null, 10)); repository.add(new MtMessage("message", null, 100)); repository.add(new MtMessage("message", null, 101)); repository.add(new MtMessage("message", null, 1001)); repository.add(//from w w w . j a va2 s . c om new MtMessage("message", Arrays.asList(new String[] { "tel:823728", "list:all_registered" }), 999)); context.checking(new Expectations() { { allowing(request).getParameter(DefaultKewordMatcher.SERVICE_KEYWORD); will(returnValue(MtLogCheckServiceHandler.MATCHING_KEYWORD)); allowing(request).getParameter(MtLogCheckServiceHandler.KEY_SINCE); will(returnValue("900")); } }); String jsonStr = handler.serve(request); JSONArray result = (JSONArray) JSONValue.parse(jsonStr); assertEquals(2, result.size()); JSONArray addresses = (JSONArray) ((JSONObject) result.get(1)) .get(MtLogCheckServiceHandler.JSON_KEY_ADDRESSES); assertEquals(2, addresses.size()); }
From source file:at.ait.dme.yuma.suite.apps.core.server.annotation.JSONAnnotationHandler.java
public static ArrayList<Annotation> parseAnnotations(String json) { ArrayList<Annotation> annotations = new ArrayList<Annotation>(); JSONArray jsonArray = (JSONArray) JSONValue.parse(json); if (jsonArray == null) return annotations; for (Object obj : jsonArray) { JSONObject jsonObj = (JSONObject) obj; Annotation annotation;/*from w ww .j a v a 2 s . c o m*/ MediaType type = MediaType.valueOf(((String) jsonObj.get(KEY_MEDIA_TYPE)).toUpperCase()); String fragment = (String) jsonObj.get(KEY_FRAGMENT); if (type == MediaType.IMAGE || type == MediaType.MAP) { annotation = new ImageAnnotation(); if ((fragment != null) && (!fragment.isEmpty())) { SVGFragmentHandler svg = new SVGFragmentHandler(); try { annotation.setFragment(svg.toImageFragment((String) jsonObj.get(KEY_FRAGMENT))); } catch (IOException e) { logger.warn("Could not parse image fragment: " + e.getMessage()); } } } else if (type == MediaType.AUDIO) { annotation = new AudioAnnotation(); if ((fragment != null) && (!fragment.isEmpty())) { AudioFragmentHandler afh = new AudioFragmentHandler(); annotation.setFragment(afh.parseAudioFramgent(fragment)); } } else { throw new RuntimeException("Unsupported annotation type: " + type.name()); } annotation.setId((String) jsonObj.get(KEY_ID)); annotation.setParentId((String) jsonObj.get(KEY_PARENT_ID)); annotation.setRootId((String) jsonObj.get(KEY_ROOT_ID)); annotation.setObjectUri((String) jsonObj.get(KEY_OBJECT_URI)); annotation.setCreated(new Date((Long) jsonObj.get(KEY_CREATED))); annotation.setLastModified(new Date((Long) jsonObj.get(KEY_LAST_MODIFIED))); annotation.setCreatedBy(parseUser((JSONObject) jsonObj.get(KEY_CREATED_BY))); annotation.setTitle((String) jsonObj.get(KEY_TITLE)); annotation.setText((String) jsonObj.get(KEY_TEXT)); annotation.setMediaType(type); String scope = (String) jsonObj.get(KEY_SCOPE); if (scope != null) { annotation.setScope(Scope.valueOf(scope.toUpperCase())); } else { annotation.setScope(Scope.PUBLIC); } JSONArray jsonTags = (JSONArray) jsonObj.get(KEY_TAGS); if (jsonTags != null) annotation.setTags(parseSemanticTags(jsonTags)); JSONArray jsonReplies = (JSONArray) jsonObj.get(KEY_REPLIES); if (jsonReplies != null) { ArrayList<Annotation> replies = parseAnnotations(jsonReplies.toString()); annotation.setReplies(replies); } annotations.add(annotation); } return annotations; }
From source file:com.johncroth.histo.logging.LogHistogramWriterParserTest.java
@Test public void testWriteAndReadHistogram() throws Exception { LogHistogram x = makeRecorder().getHistogram("foo"); String xJson = writer.convert(x).toString(); LogHistogram xParsed = parser.parseHistogramJson((JSONObject) JSONValue.parse(xJson)); LogHistogramCalculator xParsedCalc = new LogHistogramCalculator(xParsed); assertEquals(2, xParsedCalc.getTotalCount()); assertEquals(11.0, xParsedCalc.getTotalWeight(), .00001); LogHistogram empty = new LogHistogram(); String emptyJson = writer.convert(empty).toString(); LogHistogram emptyParsed = parser.parseHistogramJson((JSONObject) JSONValue.parse(emptyJson)); assertEquals(empty.getBucketMap(), emptyParsed.getBucketMap()); }
From source file:backtype.storm.utils.ShellProcess.java
public JSONObject readMessage() throws IOException { String string = readString(); JSONObject msg = (JSONObject) JSONValue.parse(string); if (msg != null) { return msg; } else {//from w ww. j a v a 2 s .com throw new IOException("unable to parse: " + string); } }
From source file:control.ParametrizacionServlets.InsertarLaboratorios.java
/** * Handles the HTTP <code>POST</code> method. * * @param request servlet request/*from w w w .ja v a 2 s .co 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 salida = new JSONObject(); try { String nombre = request.getParameter("nombre"); String direccion = request.getParameter("direccion"); String telefono = request.getParameter("telefono1"); String telefono2 = request.getParameter("telefono2"); String correo = request.getParameter("correo"); String resolucion = request.getParameter("resolucion"); String vigencia = request.getParameter("vigencia"); String contactos = request.getParameter("contactos"); String paramAcreditados = request.getParameter("paramAcreditados"); Laboratorios manager = new Laboratorios(); manager.insertar(nombre, contactos, direccion, telefono, telefono2, correo, resolucion, vigencia); AcreditacionParametros managerAcreditacion = new AcreditacionParametros(); int codigoLaboratorio = manager.getCodigoLaboratorio(); // la informacion se converte en unJSONArray Object obj = JSONValue.parse(paramAcreditados); 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); int codParametro = Integer.parseInt((String) jsonObject.get("codigoParam")); managerAcreditacion.insertar(codParametro, codigoLaboratorio); } } catch (Exception e) { //Armamos la respuesta JSON y la enviamos response.setContentType("application/json"); salida.put("error", 0); response.getWriter().write(salida.toString()); } }