【发布时间】:2016-05-08 06:09:18
【问题描述】:
我正在尝试在 HighCharts 中制作折线图,但是在将 JSON 数据绑定到图表时遇到了一些问题。我需要从 JSON 输出中删除引号以将其传递到 HighChart。数据正在从 Java 方法传递到 JavaScript 文件。无论如何,在将数组传递给 JavaScript 之前,是否要从数组中删除双引号( "" )?
我尝试在 Java 中使用 replaceAll,但它返回的数据仍然包含引号。
String result = resultSet.toString().replaceAll("\"", "");
result = result.substring(1,result.length() -1);
JSONArray finalResultset = new JSONArray();
finalResultset.put(result);
上面生成的 JSON:
["
[Date.UTC(2016, 4, 02, 00, 00 ,00), 0.0],
[Date.UTC(2016, 4, 03, 00, 00 ,00), 0.0],
[Date.UTC(2016, 4, 04, 00, 00 ,00), 0.0],
[Date.UTC(2016, 4, 05, 00, 00 ,00), 0.0],
[Date.UTC(2016, 4, 06, 00, 00 ,00), 0.0],
[Date.UTC(2016, 4, 07, 23, 43, 00), 3.0],
[Date.UTC(2016, 4, 08, 00, 00 ,00), 0.0]
"]
我也尝试使用 .slice,但它返回的是字符串值而不是数组。
下面附上我创建的接口类到Javascript
webview.addJavascriptInterface(new WebAppInterface(context), "Android");
以下方法:
var 葡萄糖 = Android.getGlucoseData();
生成以下数据:
[
["Date.UTC(2016, 4, 02, 00, 00 ,00), 0.0"],
["Date.UTC(2016, 4, 03, 00, 00 ,00), 0.0"],
["Date.UTC(2016, 4, 04, 00, 00 ,00), 0.0"],
["Date.UTC(2016, 4, 05, 00, 00 ,00), 0.0"],
["Date.UTC(2016, 4, 06, 00, 00 ,00), 0.0"],
["Date.UTC(2016, 4, 07, 23, 43, 00), 3.0"],
["Date.UTC(2016, 4, 08, 00, 00 ,00), 0.0"]
]
上面生成 JSON 的 Java 代码是:
public JSONArray getGlucoseJSON() throws JSONException {
TableControllerUser TCU = new TableControllerUser(context);
DateTime lastWeek = new DateTime();
DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd"); // for retrieval
Cursor cursor = getWeekJournals(TCU.getLoggedInId());
JSONArray resultSet = new JSONArray();
cursor.moveToFirst();
for (int i = 6; i >= 0; i--) {
String date = lastWeek.minusDays(i).toString(dtf);
Cursor c = getGlucoseForDay(date);
JSONArray holder = new JSONArray();
String glucose_time;
String[] splitted;
String [] split2;
String split3 [];
if (c.getCount() > 0) { // amount of glucose entries for the day
System.out.println("Amount of glucose for date " + date + " is " + c.getCount());
c.moveToFirst();
for (int x = 0; x < c.getCount(); x++) {
glucose_time = c.getString(c.getColumnIndexOrThrow("glucose_time"));
JSONArray arry = new JSONArray();
String finalDate = null;
//Split the date into YYYY,MM,DD,HH,SS
splitted = glucose_time.split("-");
split2 = splitted[2].split(" ");
split3 = split2[1].split(":");
int month = Integer.parseInt(splitted[1]) - 1;
finalDate = "Date.UTC("+splitted[0]+", " + month + ", " + splitted[2].substring(0,2) + ", " + split3[0] + ", " +split3[1]+ ", 00), " + c.getDouble(c.getColumnIndexOrThrow("glucose")) ;
Log.d("Glucose TIME", finalDate);
arry.put(finalDate);
resultSet.put(arry);
if (!c.isLast()) {
c.moveToNext();
Log.d("Glucose JSON", "Still has more rows.");
}
}
c.close(); // no more rows for the date
} else { // no data
int month = Integer.parseInt(date.substring(5,7)) - 1;
String noDataDate = "Date.UTC(" +date.substring(0,4) + ", " + month + ", " + date.substring(8,10) + ", 00, 00 ,00), 0.0";
holder.put(noDataDate);
resultSet.put(holder);
}
}
cursor.close();
Log.d("Glucose JSON Result", resultSet.toString());
return resultSet;
}
Java和Javascript的接口
public WebAppInterface(Context c) {
context = c;
}
/*Glucose data in JSON for past 7 days for Line 1*/
@JavascriptInterface
public JSONArray getGlucoseData() throws JSONException {
JSONArray jsonRecord;
jsonRecord = new TableControllerJournal(context).getGlucoseJSON();
Log.d("Final Glucose Result", jsonRecord.toString());
return jsonRecord;
}
我想要的 JSON 如下,以便将其呈现为 Highcharts 的系列数据。
[
[Date.UTC(2016, 4, 02, 00, 00 ,00), 0.0],
[Date.UTC(2016, 4, 03, 00, 00 ,00), 0.0],
[Date.UTC(2016, 4, 04, 00, 00 ,00), 0.0],
[Date.UTC(2016, 4, 05, 00, 00 ,00), 0.0],
[Date.UTC(2016, 4, 06, 00, 00 ,00), 0.0],
[Date.UTC(2016, 4, 07, 23, 43, 00), 3.0],
[Date.UTC(2016, 4, 08, 00, 00 ,00), 0.0]
]
【问题讨论】:
标签: javascript java android json highcharts