本文整理匯總了Java中org.json.JSONObject.write方法的典型用法代碼示例。如果您正苦於以下問題:Java JSONObject.write方法的具體用法?Java JSONObject.write怎麽用?Java JSONObject.write使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.json.JSONObject
的用法示例。
在下文中一共展示了JSONObject.write方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: doPost
import org.json.JSONObject; //導入方法依賴的package包/類
@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());
}
示例2: matchesIdToNames
import org.json.JSONObject; //導入方法依賴的package包/類
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());
}
}
示例3: writeToFiles
import org.json.JSONObject; //導入方法依賴的package包/類
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;
}
示例4: sendJsonResponse
import org.json.JSONObject; //導入方法依賴的package包/類
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());
}
示例5: sendJsonListResponse
import org.json.JSONObject; //導入方法依賴的package包/類
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());
}
}
示例6: writeDataJSON
import org.json.JSONObject; //導入方法依賴的package包/類
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;
}
示例7: write
import org.json.JSONObject; //導入方法依賴的package包/類
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();
}
}
示例8: writeJSON
import org.json.JSONObject; //導入方法依賴的package包/類
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;
}