本文整理汇总了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;
}