我有以下JSP:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="org.json.JSONObject"%>
<%@ page import="org.json.JSONArray"%>
<html>
<head>
<script src="https://code.highcharts.com/stock/highstock.js"></script>
<script src="https://code.highcharts.com/stock/modules/exporting.js"></script>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Welcome <%=request.getAttribute("resultset")%></title>
</head>
<body>
<h3>Login successful!!!</h3>
<h4>
Hello,
<%=request.getAttribute("jsondata")%>
</h4>
<p>------------</p>
<div id="container" style="height: 400px; min-width: 310px"></div>
<script type="text/javascript">
$(document).ready(function() {
var data = <%=request.getAttribute("jsondata")%>;
document.write($.parseJSON(data));
var seriesOptions = [];
// Populate series
for (i = 0; i < data.DeltaRealTime.length; i++){
seriesOptions[i] = {
name: data.DeltaRealTime[i].name,
data: [data.DeltaRealTime[i].key, parseFloat(data.DeltaRealTime[i].value)]
};
}
$('#container').highcharts({
chart: {
type: 'spline',
animation: Highcharts.svg, // don't animate in old IE
marginRight: 10,
events: {
load: function() {
// set up the updating of the chart each second
setInterval(function() {
// Populate series
for (i = 0; i < data.DeltaRealTime.length; i++){
var chart = $('#container').highcharts();
chart.series[i].addPoint([data.DeltaRealTime[i].key, parseFloat(data.DeltaRealTime[i].value)]);
}
}, 1000);
}
}
},
title: {
text: "Valori Delta"
},
xAxis: {
[...]
series: seriesOptions
});
});
</script>
</body>
</html>
但是当我运行我的应用程序时,只有“Hello”,并且打印了JSON值和虚线“---”。 JavaScript只是原始转移到HTML代码中:/
如果我改变
document.write($.parseJSON(data));
到
document.write(data);
输出HTML文档只是:
[object Object]