gpt4 book ai didi

java - 解析 JSON 以供在 highchart.js 中使用

转载 作者:太空宇宙 更新时间:2023-11-04 12:54:44 24 4
gpt4 key购买 nike

我从数据库中提取多个值,并在 servlet 中对它们进行计算,并将这些值作为响应传递回我的前端,然后前端使用 highchart.js 来可视化数据。

目前,我以 highchart 可以使用的 JSON 对象形式创建一个字符串,但我想通过使用 GSON 或 Jackson 等 libaray 来完善我的代码显然,这将更不容易出错并且更可重用,但我不确定我是否能做到。

我当前的解析方法:

        this.json = "[";
this.zoneId = ZoneId.of("Europe/Dublin");
this.map = new LinkedHashMap<>();
this.dayCount = 0;
this.end = "[" + (to.toInstant().getEpochSecond() * TimestampUtils.MILLIS_PER_SECOND + "," + null + "]]}");

public String Generate(String type) {

// Check that we have dates to work with
if (sDate != null && eDate != null) {

// Pull data from DB
try {
throughPutList = dbWork.getThroughputEntriesInTimespan(env, app, from, to, connection);
} catch (Exception e) {
e.printStackTrace();
}

String[] calculations = new String[]{"default", "total"};

assert throughPutList != null;
int count = 1;
for (String calc : calculations) {
for (ThroughputEntry chartModel : throughPutList) {

// Convert the epochSecond value of the period pulled for the DB to it's day for insertion into a Map
ZonedDateTime zdt = ZonedDateTime.ofInstant(chartModel.getRetrieved(), zoneId);
LocalDate localDate = zdt.toLocalDate();
entryDay = localDate.atStartOfDay(zoneId).toEpochSecond() * TimestampUtils.MILLIS_PER_SECOND;
valueForDate = map.get(entryDay);

if (count == 1) {
json += "{\"name\": \"" + calc + "\", \"data\":[[" + (from.toInstant().getEpochSecond() * TimestampUtils.MILLIS_PER_SECOND + "," + null + "],");
}
if (calc.equals("default")) {
Default(chartModel);
} else if (calc.equals("total")) {
Total(chartModel);
}
if (count == throughPutList.size()) {

for (Map.Entry<Long, BigDecimal> entry : map.entrySet()) {

json += "[" + (entry.getKey() + "," + entry.getValue() + "],");
}
if (calc != "total") {
json += end + ",";

} else {
json += end;
}

map.clear();
count = 0;
}
count++;
}
}
json += "]";

if (throughPutList == null) {
json = "No record found";
}
} else {
json = "Date must be selected." + "App : " + app + " " + eDate;
}
return json;
}

public void Default(ThroughputEntry chartModel) {

map.put(chartModel.getRetrieved().getEpochSecond() * TimestampUtils.MILLIS_PER_SECOND, BigDecimal.valueOf(chartModel.getThroughput()));
}

public void Total(ThroughputEntry chartModel) {

newTotalForDate = (null == valueForDate) ? BigDecimal.valueOf(chartModel.getThroughput()) :
valueForDate.add(BigDecimal.valueOf(chartModel.getThroughput()));

if (null == newTotalForDate) { // If we failed to get new value.
// TODO: Handle error condition.
} else { // Else normal, we have a new total. Store it in map.

map.put(entryDay, newTotalForDate); // Replaces any old value.s
}
}

因此,库需要使用 String 值和 linkedHashMap 来填充 JSON 响应

响应 JSON 需要采用以下形式:

[{"name": "mean", "data":[[1454806080000,null],[1454889600000,204.043],[1454976000000,202.281],[1455062400000,160.493],[1455148800000,206.000],[1455235200000,229.917],[1455321600000,82.430],[1455408000000,87.242],[1455494400000,223.584],[1455580800000,188.412],[1455667200000,228.199],[1455753600000,224.268],[1455840000000,198.513],[1455842880000,null]]},{"name": "extrapo", "data":[[1454806080000,null],[1455842880000,null]]},{"name": "max", "data":[[1454806080000,null],[1455842880000,null]]}]

实践中的响应将包含更多数据,约 126 组。

我正在使用这个客户端:

// Function for loading data via AJAX and showing it on the chart
function ajaxLoadChart(startDate, endDate, appName, envName) {
// If no data is passed (the chart was cleared)
if (!startDate || !endDate) {
return;
}
// Otherwise, issue an AJAX request
$.ajax({
url: 'http://myServlet.com/operations.PreProcessor',
crossDomain: true,
async: true,
type: "GET",
dataType: "json",
data: {
start: startDate,
end: endDate,
env: envName,
app: appName
},
success: function (json) {

options.series[0] = json[0];
options.series[1] = json[1];
options.series[2] = json[2];
chart = new Highcharts.Chart(options);
},
error: function (xhr, status, error) {
alert(status + " " + error);
console.log(xhr);
}
})
}

是否可以使用提到的库之一来实现这一目标?

最佳答案

我想我明白你想要实现的目标,并且我自己也做过类似的事情。

要做的事情是创建一些 POJOS(Plain Ol' Java Oblects),它代表您当前在 JSON 中创建的数据结构。它们应该具有与当前 JSON 结构相同的成员名称、子级等。然后构建它,然后将其传递给 GSON 以序列化为 JSON。

我已经完成了此操作,并调用我创建的“ View 模型”对象,然后将其作为 JSON 字符串传递给客户端。

关于java - 解析 JSON 以供在 highchart.js 中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35509115/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com