Java Code Examples for org.json.JSONObject.write()

The following are Jave code examples for showing how to use write() of the org.json.JSONObject class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: Avalon-Executive   File: GetAllLang.java   View Source Code Vote up 7 votes
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    req.setCharacterEncoding("UTF-8");
    resp.setCharacterEncoding("UTF-8");
    JSONObject object = new JSONObject();
    JSONArray array = new JSONArray();
    List<Language> allLanguage = LanguagePool.getAllLanguage();
    for (Language thisLanguage : allLanguage) {
        JSONObject child = new JSONObject();
        child.put("id", thisLanguage.getId());
        child.put("name", thisLanguage.getName());
        array.put(child);
    }
    object.put("lang", array);
    resp.setContentType("application/json");
    object.write(resp.getWriter());
}
 
Example 2
Project: CF-rating-prediction   File: AdditionalExecutor.java   View Source Code Vote up 6 votes
private static void matchesIdToNames(String[] args) {
    boolean gym = Boolean.getBoolean(args[1]);
    List<String> contests = CodeForcesSDK.getContestNames(gym);
    List<JSONObject> list = new ArrayList<>(contests.size());
    for (int contestId = contests.size() - 1; contestId > 0; contestId--) {
        JSONObject json = new JSONObject();
        json.put("contestId", contestId);
        json.put("name", contests.get(contestId));
        list.add(json);
    }

    JSONObject contestNames = new JSONObject();
    contestNames.put("status", "OK");
    contestNames.put("result", new JSONArray(list));

    String fileName = "contests/contestNames.json";
    try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(fileName))) {
        contestNames.write(writer);
        writer.write("\n");
    } catch (Exception ex) {
        System.err.println("Couldn't write contestNames\n"
                + ex.getMessage());
    }
}
 
Example 3
Project: CF-rating-prediction   File: PastRatingDownloader.java   View Source Code Vote up 6 votes
private static boolean writeToFiles(String filePrefix, TreeMap<String, Integer> rating, String contestId) {
    boolean result = true;
    String fileName = getFileName(contestId, filePrefix);
    JSONObject json = toJSON(rating);

    try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(fileName))) {
        json.write(writer, 2, 0);
        writer.write("\n");
    } catch (Exception ex) {
        System.err.println("Couldn't write past rating to the file\n"
                + ex.getMessage());
        result = false;
    }

    return result;
}
 
Example 4
Project: logistimo-web-service   File: JsonRestServlet.java   View Source Code Vote up 5 votes
protected void sendJsonResponse(HttpServletResponse response, int statusCode, JSONObject jsonObj)
    throws IOException, JSONException {
  response.setStatus(statusCode);
  response.setContentType(JSON_UTF8);
  //jsonObj.writeJSONString(response.getWriter()); // used earlier with json.simple
  jsonObj.write(response.getWriter());
}
 
Example 5
Project: logistimo-web-service   File: JsonRestServlet.java   View Source Code Vote up 5 votes
protected void sendJsonListResponse(HttpServletResponse response, int statusCode,
                                    List<JSONObject> jsonObjList)
    throws IOException, JSONException {
  response.setStatus(statusCode);
  response.setContentType(JSON_UTF8);
  Iterator<JSONObject> it = jsonObjList.iterator();
  while (it.hasNext()) {
    JSONObject jsonObj = it.next();
    //jsonObj.writeJSONString(response.getWriter()); // used earlier with json.simple
    jsonObj.write(response.getWriter());
  }
}
 
Example 6
Project: SWET   File: Utils.java   View Source Code Vote up 5 votes
public String writeDataJSON(Map<String, String> data, String defaultPayload) {
	String payload = defaultPayload;
	JSONObject json = new JSONObject();
	try {
		for (String key : data.keySet()) {
			json.put(key, data.get(key));
		}
		StringWriter wr = new StringWriter();
		json.write(wr);
		payload = wr.toString();
	} catch (JSONException e) {
		System.err.println("Exception (ignored): " + e);
	}
	return payload;
}
 
Example 7
Project: massim   File: ReplayWriter.java   View Source Code Vote up 5 votes
private void write(String startTime, String simId, String name, JSONObject json) {
    String prefix = startTime + "-" + simId;
    File file = Paths.get(this.replayPath, prefix, name + ".json").toFile();
    File dir = file.getParentFile();
    if (!dir.exists()) dir.mkdirs();

    try {
        FileWriter writer = new FileWriter(file);
        json.write(writer);
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example 8
Project: northpine   File: ScrapeJob.java   View Source Code Vote up 5 votes
private String writeJSON(JSONObject obj) {
  // e.g. 'output/wetlands1.json'
  String outFile = outputFileBase + current.incrementAndGet() + ".json";
  log.info(outFile);
  try ( BufferedWriter br = new BufferedWriter( new FileWriter( new File( outFile ) ) ) ) {
    obj.write(br);
  } catch ( IOException e ) {
    log.error("Couldn't write '" + outFile + "'", e);
    failJob( "Failed to write a response.. our fault, try again?" );
  }
  return outFile;
}