【问题标题】:HighCharts JSONArray without quotesHighCharts JSONArray 不带引号
【发布时间】: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


    【解决方案1】:

    您当前在 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 中计算 epoch(以毫秒为单位的时间),而不是将其作为必须在 Javascript 中评估的字符串传递。如果 Calendar 不能满足您的需求,Java 有多种方法可以获取纪元。

    【讨论】:

    • 谢谢,以毫秒为单位设置时间是解决方案!
    猜你喜欢
    • 2013-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多