HighCharts JSONArray没有引号

时间:2016-05-08 06:09:19

标签: javascript java android json highcharts

我试图在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但它返回一个String值而不是一个数组。

将我创建的接口类附加到Javascript

  

webview.addJavascriptInterface(新的WebAppInterface(上下文)," 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之间的接口

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

1 个答案:

答案 0 :(得分:3)

您当前正在使用Java向JSONArray添加一个String并传递它,然后想要在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中进行评估的String传递。如果日历不符合您的需求,Java有几种方法可以获得时代。