简体   繁体   中英

HighCharts JSONArray without quotes

I'm trying to make a line chart in HighCharts, however I'm having some problem binding JSON data to the chart. I need to remove the quotes from the JSON output to pass it into HighChart. The data is being passed from a Java method to the JavaScript file. Is there anyway to remove the double quotes ( "" ) from the array before it is passed to the JavaScript?

I tried to use replaceAll in Java but the data it returns still contains quotes.

 String result = resultSet.toString().replaceAll("\"", "");
        result = result.substring(1,result.length() -1);
        JSONArray finalResultset = new JSONArray();
        finalResultset.put(result);

The JSON the above produces:

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

I also attempted to use .slice but it returns a String value instead of an array.

Following attaches the interface class I created to Javascript

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

The following method:

var glucose = Android.getGlucoseData();

generates the following data:

[
["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 code to produce JSON above is:

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;
}

The interface between Java and 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;
    }

The JSON I want is as follow in order to render it into Highcharts's series data.

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

You are currently adding a String to a JSONArray in Java and passing it, then want to evaluate it in the Javascript. One alternative is to just evaluate these results in Java instead.

For example, you currently (summarized) have:

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);

Instead you could:

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);

So in essence just calculate the epoch (time in milliseconds) in Java instead of passing it as a String that has to be evaluated in Javascript. Java has several alternatives to getting the epoch if Calendar doesn't suit your needs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM