cuốn sách gpt4 ai đã làm

java - Google Chart JSON 问题 - Java

In lại Tác giả: Walker 123 更新时间:2023-11-29 05:46:49 26 4
mua khóa gpt4 Nike

我正在使用 jquery 从 servlet 返回一个基本的 JSON 对象,它工作得非常好,但是我在格式化 JSON 以实际填充图表时遇到了问题。

这是调用 servlet 的代码:

google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {

$.getJSON('SearchServlet', {servletAction: 'getSummary'}, function(json) {
var data = new google.visualization.DataTable(json);
var chart = new google.visualization.PieChart(document.getElementById('summaryPieChart'));
chart.draw(data, {backgroundColor:'#e9f1f4',width: '90%', height: '90%', legend:{position:'none'}, chartArea:{width:"90%",height:"90%"},colors:['#94d784','#d78484']});
});

}

在 servlet 端,我用来创建 JSON 的代码是:

private int positive;
private int negative;
private int neutral;

public DataTable createResultsJSON(){
DataTable data = new DataTable();
ArrayList cols = new ArrayList();
cols.add(new ColumnDescription("summary", ValueType.TEXT, "Summary"));
cols.add(new ColumnDescription("result", ValueType.NUMBER, "Result"));

data.addColumns(cols);

thử {
data.addRowFromValues("positive", positive, true);
data.addRowFromValues("negative", negative, true);
data.addRowFromValues("neutral", neutral, true);
} catch (TypeMismatchException e) {
System.out.println("Invalid type!");
}
trả về dữ liệu;
}

使用以下方法将其转换为 JSON:

String resultJson = new Gson().toJson(createResultsJSON());

结果 JSON 的示例是:

{
"columns":[
{"id":"summary","type":"TEXT","label":"Summary","pattern":""},
{"id":"result","type":"NUMBER","label":"Result","pattern":""}],
"columnIndexById":{"result":1,"summary":0},
"rows":[
{"cells":[{"value":{"value":"positive"}},{"value":{"value":362.0}}]},
{"cells":[{"value":{"value":"negative"}},{"value":{"value":302.0}}]},
{"cells":[{"value":{"value":"neutral"}},{"value":{"value":349.0}}]}],
"warnings":[]
}

但是,根据 Google Chart JSON 规范,它应该类似于以下内容:

{
"cols": [
{"id":"","label":"Topping","pattern":"","type":"string"},
{"id":"","label":"Slices","pattern":"","type":"number"}
],
"rows": [
{"c":[{"v":"Mushrooms","f":null},{"v":3,"f":null}]},
{"c":[{"v":"Onions","f":null},{"v":1,"f":null}]},
{"c":[{"v":"Olives","f":null},{"v":1,"f":null}]},
{"c":[{"v":"Zucchini","f":null},{"v":1,"f":null}]},
{"c":[{"v":"Pepperoni","f":null},{"v":2,"f":null}]}
]

我已经尝试了多种不同的方法来在服务器端正确格式化 JSON,但仍然卡住了,我相信这是一个快速简单的修复方法,因此我们将不胜感激。

当查询获取 JSON 并尝试处理它时,它显示“表没有列”

Cảm ơn

câu trả lời hay nhất

创建字符串而不是 json。该字符串放入请求范围并直接在 jsp(request.getAttribute()); 中获取该字符串;

var data = new google.visualization.DataTable(request.getAttribute());

我做了这个我得到了答案..

关于java - Google Chart JSON 问题 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15571185/

26 4 0
Walker 123
Hồ sơ

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá taxi Didi miễn phí
Phiếu giảm giá taxi Didi
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress