javascript - HighCharts JSONArray 不带引号

标签 javascript java android json highcharts

我正在尝试在 HighCharts 中制作折线图,但是在将 JSON 数据绑定(bind)到图表时遇到一些问题。我需要从 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 但它返回一个字符串值而不是数组。

下面将我创建的接口(interface)类附加到 Javascript

webview.addJavascriptInterface(new WebAppInterface(context), "Android");

以下方法:

var glucose = 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 之间的接口(interface)

  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]
]

最佳答案

您当前正在 Java 中将字符串添加到 JSONArray 并传递它,然后想要在 Javascript 中对其进行计算。一种替代方法是仅在 Java 中评估这些结果。

例如,您目前(总结)有:

JSONArray arry = new JSONArray();
String finalDate = null;
finalDate = "Date.UTC("+splitted[0]+", " + month + ", " + splitted[2].substring(0,2) + ", " + split3[0] + ", " +split3[1]+ ", 00), " + c.getDouble(c.getColumnIndexOrThrow("glucose")) ;
arry.put(finalDate);

// ...
resultSet.put(arry);

相反,你可以:

int year = Integer.parseInt(splitted[0]);
int month = Integer.parseInt(splitted[1]) - 1;
int day = Integer.parseInt(splitted[2].substring(0,2));
int hours = Integer.parseInt(split3[0]);
int minutes = Integer.parseInt(split3[1]);

JSONArray arry = new JSONArray();
Calendar cal = Calendar.getInstance();
cal.set(year, month, day, hours, minutes);
arry.put(cal.getTimeInMillis());
arry.put(c.getDouble(c.getColumnIndexOrThrow("glucose"));

// ...
resultSet.put(arry);

因此本质上只是在 Java 中计算纪元(以毫秒为单位的时间),而不是将其作为必须在 Javascript 中计算的字符串传递。如果日历不能满足您的需求,Java 有多种获取纪元的替代方法。

关于javascript - HighCharts JSONArray 不带引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37096826/

相关文章:

javascript - AngularJS Jasmine 测试: scoped variable always undefined

java - GridLayout 内容不显示

java - ORMlite,如果我*删除*注释会更快吗?

android - 我究竟做错了什么?将 BluetoothDevice 的 ArrayList 保存到 SharedPreferences

javascript - MailApp指定邮箱发件人

javascript - Javascript 中的高密度随机字符串

javascript - 过滤子数组对象

java - 在java文件中使用密码安全吗?

android - WakeLock 已完成,但仍然存在错误,即使我正在释放它

Android:关于GPS的各种问题