我怎样才能将jsonArray从jsp解析为Javascript

时间:2014-10-26 04:47:42

标签: java jquery json jsp

这是我的代码:

Display.jsp:

    <%
    JSONArray jsonArray = new JSONArray();
    String location = request.getParameter("location");
    Class.forName(OracleDriver.class.getName().toString());
    Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","ram","ram");
    PreparedStatement preparedStatement = connection.prepareStatement("select * from job where location = ?");
    preparedStatement.setString(1,location);
    ResultSet resultSet = preparedStatement.executeQuery();
    int index = 0;
    if(!resultSet.next()){
            jsonArray.add(0, new Job("NA","NA","NA","NA","NA"));
            out.println(jsonArray);
    }else{
        while(resultSet.next()){
            index ++;
            Job job = new Job(resultSet.getString("title"),resultSet.getString("location"),resultSet.getString("eligibility"),resultSet.getString("position"),resultSet.getString("pdate"));
            jsonArray.add(index, job);
        }
        out.println(jsonArray);
    }

%>

包含Job对象列表的jsonArray, 我如何解析javascript中的jsonArray

任何想法......

1 个答案:

答案 0 :(得分:2)

一个建议可能是将隐藏的dom元素附加到javascript变量,如p或隐藏输入。

然后,将内部html设置为json数组

<p id="myJsonArray" style="display:none;"> <%= jsonArray %> </p>

然后最后,附上一些事件,触发一个javascript函数来读取那个

的内部html ...如果你想要它是自动的,可能是一个定时的间隔或者某些东西来检查那个元素是否改变了

我会诚实地忘记所有这一切,只是写一个网络服务(使用运动衫或其他东西),并创建一个ajax调用来获得你想要的东西:)但是,这是我的意见。

如果您想要任何进一步的想法,请告诉我们!

  

这里有一些工作代码。 我还是要强调,这可能不是最好的方法。将这么多代码混合在一个html页面中很难维护,并且不可读。我发现这个链接到另一个StackOverflow帖子。值得一读。 https://netbeans.org/kb/docs/javaee/ecommerce/design.html#architecture
  StackOverflow帖子:JSF vs Facelets vs JSP

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

<%@ page import="java.util.List,java.util.ArrayList,org.json.JSONArray,java.util.Arrays" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>test</title>
<!-- DataTables CSS -->
<link rel="stylesheet" type="text/css" href="//cdn.datatables.net/1.10.3/css/jquery.dataTables.css">

<!-- jQuery -->
<script type="text/javascript" charset="utf8" src="//code.jquery.com/jquery-1.10.2.min.js"></script>

<!-- DataTables -->
<script type="text/javascript" charset="utf8" src="//cdn.datatables.net/1.10.3/js/jquery.dataTables.js"></script>

<script>
$(function(){
    //If your data won't change, you could just draw the table immediately after the page loads.
    var testTable = $("#dynamicTable").DataTable({
        data : JSON.parse($("#hiddenJson").html())
    });

    //If you're going to be updating data dynamically, you might 
    $("#refresh").click(function(){
        var data = JSON.parse($("#hiddenJson").html());

        testTable.clear();
        var modifiedData;
        for(r in data) {
            modifiedData = data[r];
            modifiedData[0] += " dynamically added";
            modifiedData[1] += " dynamically added";
            modifiedData[2] += " dynamically added";
            testTable.row.add(modifiedData);        
        };              
        testTable.draw();
    });
});

</script>
</head>
<body>
<%

    JSONArray jsonArray = new JSONArray();
    String location = request.getParameter("location");
   //  Class.forName(OracleDriver.class.getName().toString());
   // Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","ram","ram");
   // PreparedStatement preparedStatement = connection.prepareStatement("select * from job where location = ?");
   // preparedStatement.setString(1,location);
   // ResultSet resultSet = preparedStatement.executeQuery();
    //int index = 0;
    //if(!resultSet.next()){
    //        jsonArray.add(0, new Job("NA","NA","NA","NA","NA"));
    //        out.println(jsonArray);
    //}else{
    //    while(resultSet.next()){
    //        index ++;
    //        Job job = new Job(resultSet.getString("title"),resultSet.getString("location"),resultSet.getString("eligibility"),resultSet.getString("position"),resultSet.getString("pdate"));
    //        jsonArray.add(index, job);
    // }    
    jsonArray.put(0, ("NA,NA,NA").split(",")); 
    jsonArray.put(1, ("NA2,NA2,NA2").split(",")); 
    jsonArray.put(2, ("NA3,NA3,NA3").split(",")); 

%>

<p id="hiddenJson" style="display:none;"> <%= jsonArray.toString() %> </p>

<table id="dynamicTable">
  <thead>
    <th>column 1</th>
    <th>column 2</th>
    <th>column 3</th>
  </thead>
</table>

<button id="refresh"> Update Data </button>
</body>
</html>