List of usage examples for org.json.simple JSONObject writeJSONString
public void writeJSONString(Writer out) throws IOException
From source file:com.mydomain.teavmfirstapp.Server.java
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, FileNotFoundException, IOException { try {/*from w w w.j av a2 s . c o m*/ Thread.sleep(500); } catch (InterruptedException e) { return; } String action = req.getHeader("action"); if (action.equals("loadData") == true) { try { JSONObject json = (JSONObject) parser.parse( new InputStreamReader(getClass().getClassLoader().getResourceAsStream("/com/DBjson.json"))); json.writeJSONString(resp.getWriter()); } catch (ParseException e) { e.printStackTrace(); } } else if (action.equals("saveData") == true) { String JSON = req.getHeader("JSON"); try { File myFile = new File(getClass().getClassLoader().getResource("/com/DBjson.json").getPath()); FileOutputStream fileStream = new FileOutputStream(myFile, false); byte[] myBytes = JSON.getBytes(); fileStream.write(myBytes); fileStream.close(); System.out.println( "? ?. , ? - "); try { JSONObject json = (JSONObject) parser.parse(new InputStreamReader( getClass().getClassLoader().getResourceAsStream("/com/DBjson.json"))); System.out.println(json.toJSONString()); } catch (ParseException e) { e.printStackTrace(); } resp.getHeader("NAME"); } catch (Exception e) { e.printStackTrace(); } } }
From source file:com.gunjan.businessLayer.dictServlet.java
protected String getJSONObjectForWord(String word) throws IOException { DBConnection conn = DBConnection.getConnection(); ArrayList defs = conn.getDefinition(word); if (defs == null) { defs = new ArrayList(); }//from w w w. java 2 s. c o m Iterator<Definition> it = defs.iterator(); JSONArray defList = new JSONArray(); while (it.hasNext()) { Definition d = (Definition) it.next(); JSONObject obj = new JSONObject(); obj.put("type", d.getType()); obj.put("definition", d.getDefinition()); defList.add(obj); } JSONObject mainObj = new JSONObject(); mainObj.put("word", word); mainObj.put("definitions", defList); StringWriter out = new StringWriter(); mainObj.writeJSONString(out); String jsonText = out.toString(); return jsonText; }
From source file:com.github.lgi2p.obirs.utils.JSONConverter.java
/** * Convert a given ObirsQuery object into a JSON format * * @param query/*from w w w . ja v a 2 s . c o m*/ * @return * @throws IOException */ public static String jsonifyObirsQuery(ObirsQuery query) throws IOException { JSONObject jsonObject = new JSONObject(); jsonObject.put("aggregator", query.getAggregator()); jsonObject.put("aggregatorParameter", query.getAggregatorParameter()); jsonObject.put("similarityMeasure", query.getSimilarityMeasure()); jsonObject.put("scoreThreshold", query.getScoreThreshold()); jsonObject.put("numberOfResults", query.getNumberOfResults()); StringWriter jsonQuery = new StringWriter(); JSONArray jsonConcepts = new JSONArray(); for (Map.Entry<URI, Double> queryConcept : query.getConcepts().entrySet()) { JSONObject jsonConcept = new JSONObject(); URI conceptURI = queryConcept.getKey(); jsonConcept.put("uri", conceptURI.stringValue()); jsonConcept.put("weight", queryConcept.getValue()); jsonConcepts.add(jsonConcept); } jsonObject.put("concepts", jsonConcepts); jsonObject.writeJSONString(jsonQuery); return jsonQuery.toString(); }
From source file:workspace.java
private void importlast(PrintWriter out, String class1, String hour) throws Exception { String[] students = util.getStudents(class1); String[] pa = util.getpa(class1, hour); JSONArray ja = new JSONArray(), ja1 = new JSONArray(); JSONObject mainObj = new JSONObject(); ja = getJsonArrayFromStringArray(students, pa); mainObj.put("students", ja); StringWriter out1 = new StringWriter(); mainObj.writeJSONString(out1); String jsonText = out1.toString(); out.println(jsonText);//w ww . j ava 2s .c o m }
From source file:edu.vt.vbi.patric.portlets.CircosGenomeViewerPortlet.java
@SuppressWarnings("unchecked") public void serveResource(ResourceRequest request, ResourceResponse response) throws PortletException, IOException { String imageId = request.getParameter("imageId"); String trackList = request.getParameter("trackList"); JSONObject res = new JSONObject(); res.put("success", true); res.put("imageId", imageId); res.put("trackList", trackList); PrintWriter writer = response.getWriter(); res.writeJSONString(writer); writer.close();//from w w w . j a va 2 s. co m }
From source file:workspace.java
private void current(PrintWriter out, String class1) throws Exception { String[] students = util.getStudents(class1); JSONArray ja = new JSONArray(), ja1 = new JSONArray(); JSONObject mainObj = new JSONObject(); ja = getJsonArrayFromStringArray(students, util.pa); mainObj.put("students", ja); StringWriter out1 = new StringWriter(); mainObj.writeJSONString(out1); String jsonText = out1.toString(); out.println(jsonText);/* w w w. j a va2s. c o m*/ }
From source file:controller.Json.java
@SuppressWarnings("unchecked") public String nestedJson(State state, JSONObject message) { JSONObject jsonParse = new JSONObject(); this._state = (state.equals(State.ERROR)) ? "ERROR" : "SUCCEEDED"; jsonParse.put("STATE", _state); jsonParse.put("MESSAGE", message); _json = new StringWriter(); try {//from w w w . j a v a 2 s.com jsonParse.writeJSONString(_json); } catch (IOException e) { e.printStackTrace(); } return _json.toString(); }
From source file:controller.Json.java
/** * Outputs JSON string with given message, specify state {Error: something * went wrong, Passed: everything is OK} * /* w ww.jav a 2s . com*/ * @param state * @param message * @return * @throws IOException */ @SuppressWarnings("unchecked") public String createJson(State state, String message) { JSONObject jsonParse = new JSONObject(); this._state = (state.equals(State.ERROR)) ? "ERROR" : "SUCCEEDED"; jsonParse.put("STATE", _state); jsonParse.put("MESSAGE", message); _json = new StringWriter(); try { jsonParse.writeJSONString(_json); } catch (IOException e) { e.printStackTrace(); } return _json.toString(); }
From source file:eumetsat.pn.common.ISO2JSON.java
@SuppressWarnings("unchecked") private Path createInfoToIndex(Path aSourceDirPath, Path aDestDirPath, long limit) { log.info("Transforming XML in {} to JSON in {}", aSourceDirPath, aDestDirPath); DocumentBuilder builder;// w w w .j a v a2 s . c o m try { DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); builder = builderFactory.newDocumentBuilder(); } catch (ParserConfigurationException e) { log.error("Error transforming directory", e); return aDestDirPath; } Collection<File> inputFiles = FileUtils.listFiles(aSourceDirPath.toFile(), null, false); int counter = 0; for (File file : inputFiles) { try { FileInputStream fileInput = new FileInputStream(file); Document xmlDocument = builder.parse(fileInput); JSONObject json = convert(xmlDocument); json.writeJSONString(writer); log.trace("JSON Result Object: {}", writer.toString()); String fName = aDestDirPath + "/" + FilenameUtils.getBaseName(file.getName()) + ".json"; FileUtils.writeStringToFile(new File(fName), json.toJSONString()); log.debug("Wrote metadata with id {} (file {}) as json to {} ", json.get(FILE_IDENTIFIER_PROPERTY), file, fName); counter++; } catch (SAXException | IOException | XPathExpressionException e) { log.error("Error transforming file {}", file, e); } if (counter >= limit) { log.warn("Stopping indexing because of limit {}", limit); break; } } log.info("Transformed {} of {} files", counter, inputFiles.size()); return aDestDirPath; }
From source file:com.releasequeue.server.ReleaseQueueServer.java
private Object postJsonRequest(URL url, JSONObject payload) throws IOException { HttpPost request = new HttpPost(url.toString()); setAuthHeader(request);/* w w w . j av a2s . co m*/ CloseableHttpClient httpClient = HttpClients.createDefault(); try { StringWriter data = new StringWriter(); payload.writeJSONString(data); StringEntity params = new StringEntity(data.toString()); request.addHeader("content-type", "application/json"); request.setEntity(params); HttpResponse response = httpClient.execute(request); StatusLine statusLine = response.getStatusLine(); int statusCode = statusLine.getStatusCode(); if (statusCode >= 400) { throw new HttpException(statusLine.getReasonPhrase()); } String json_string = EntityUtils.toString(response.getEntity()); JSONParser parser = new JSONParser(); return parser.parse(json_string); } catch (ParseException pe) { throw new RuntimeException("Failed to parse json responce", pe); } finally { httpClient.getConnectionManager().shutdown(); } }