这是json文件。
{
"data": [
{
"name": "kasun",
"position": "Software Engineer",
"salary": "10000",
"start_date": "2015/08/10",
"office": "ABC",
"extn": "100"
} ,
{
"name": "Amal",
"position": "System Architect",
"salary": "10000",
"start_date": "2015/08/10",
"office": "DEF",
"extn": "100"
}
]
}
}
在我的项目中,我必须通过servlet文件获取此json文件。 现在我已经实现了它。
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, ParseException {
HttpSession session = request.getSession();
String filePath = "D:\\yakaari\\EPIC_PROJECTS\\2015\\IBWEB\\IBWeb\\web\\datagrid.json";
try {
FileReader reader = new FileReader(filePath);
JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);
JSONArray jsonarray = (JSONArray) jsonObject.get("data");
session.setAttribute("jsonarray", jsonarray);
for (int i = 0; i < jsonarray.size(); i++) {
System.out.println("The " + i + " element of the array: " + jsonarray.get(i));
}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} catch (ParseException ex) {
ex.printStackTrace();
} catch (NullPointerException ex) {
ex.printStackTrace();
}
RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/customer/customergrid.jsp");/*redirect to the login page*/
rd.include(request, response);
}
现在问题是我想将这个json对象数组再次转换为json文件,因为我需要ajax函数的json文件类型变量。
这是ajax功能。
<script type="text/javascript">
$(document).ready(function() {
$('#example').dataTable({
"ajax": "datagrid.json",
"columns": [
{"data": "name"},
{"data": "position"},
{"data": "office"},
{"data": "extn"},
{"data": "start_date"},
{"data": "salary"}
]
,
responsive: true
});
});
</script>
是可以做到还是有其他办法?感谢