使用Json过程中的一些问题

@RequestMapping(value = "doAddFolder", method = RequestMethod.POST)
public void AddFolder(HttpServletRequest request,
HttpServletResponse response) {
String userName = (String) request.getSession().getAttribute("account");
String name = request.getParameter("name");
String path = request.getParameter("path");
// String date = testcaseService.createFolder(path, name);
String date = "";
String msg = "";
try {
date = fileService.addFile(userName, path, name);
} catch (Exception e) {
msg = "You are not allowed to create new file here.";
}
JSONObject jsonobj = new JSONObject();
jsonobj.put("user_runner", Constants.USER_ROLE_RUNNER);
jsonobj.put("msg", msg);
jsonobj.put("date", date);
this.sendJson(response, jsonobj);
}

public void sendJson(HttpServletResponse response, JSONObject jsonobj) {
try {
PrintWriter out = response.getWriter();
out.print(jsonobj);
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}

}

 

如果 报NoClassDefFoundError,也可使用:

public void getAvailableAgent(HttpServletRequest request,
HttpServletResponse response) {
String path = request.getParameter("path");
path = path+":pht";
// JSONObject jsonObj = new JSONObject();
// jsonObj.put("path", path);
Gson gson = new Gson();
String jsonObj = gson.toJson(path);
System.out.println("path:"+path);
this.sendJson(response, jsonObj);
}

public void sendJson(HttpServletResponse response, String jsonobj) {
try {
PrintWriter out = response.getWriter();
out.print(jsonobj);
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}

}

 

posted on 2017-04-07 10:37  damoguying  阅读(176)  评论(0编辑  收藏  举报