我需要在JSON对象data
下面初始化var p = ${jsonobj};
变量,并不完全确定如何在javascript中执行此操作。一个小小的上下文我在JSP页面中使用它并使用Spring MVC从Java控制器传递JSON对象。我测试了它并确保我得到正确的json对象,只是不确定初始化它的javascript语法。这个页面描述了我尝试做什么http://microbuilder.io/blog/2016/01/10/plotting-json-data-with-chart-js.html然而没有说明如何使用变量
<script>
var ctx = document.getElementById("myChart").getContext('2d');
var p = ${jsonobj};
for (var key in p) {
if (p.hasOwnProperty(key)) {
console.log(key + " -> " + p[key]);
}
}
var scatterChart = new Chart(ctx, {
type: 'scatter',
data: {
datasets: [{
label: 'Scatter Dataset',
data: [{
x: -10,
y: 0
}, {
x: 0,
y: 10
}, {
x: 10,
y: 5
}]
}]
},
options: {
scales: {
xAxes: [{
type: 'linear',
position: 'bottom'
}]
}
}
});
</script>
这是传递给jsp的JSON对象
public void testCorrelationTable(List<Test> tests,Model model)
{
JSONObject obj = new JSONObject();
System.out.print(obj);
SampleStatsCalculator sampleStatsCalculator = new SampleStatsCalculator();
SampleStatsTabulator sampleStatsTabulator = new SampleStatsTabulator(tests);
TestStatsTabulator testStatsTabulator=new TestStatsTabulator(tests);
Map<String, Map<String, Test>> sampleTestMap = sampleStatsTabulator.tabulate();
TestStatsCalculator testStatsCalculator=new TestStatsCalculator(tests,sampleTestMap);
Map <Test,Stats> testStatsMap= testStatsTabulator.tabulate();
Map <Test,Stats> testStatsnewMap=testStatsCalculator.calculateCorrelation(testStatsMap);
TestVisitor visitor=new TestVisitor();
model.addAttribute("teststatsmap",testStatsMap);
model.addAttribute("visitor",visitor );
model.addAttribute("jsonobj", obj);
for (Entry<Test, Stats> entry : testStatsnewMap.entrySet()) {
entry.getValue().accept(visitor);
obj.put(entry.getKey().getTestId(),visitor.getGroupMean());
System.out.println(entry.getKey().getTestId()+visitor.getGroupMean());
}